滚动条监听

 1 function procScroll(selector, xPercent, xCallback, yPercent, yCallback) {
 2     var _isType = function(obj, type) {
 3         return Object.prototype.toString.call(obj) === '[object ' + type + ']';
 4     };
 5 
 6     var isString = _isType(selector, 'String');
 7     if (!isString || (isString && (selector.length < 2))) {
 8         return null;
 9     }
10 
11     if ((xPercent <= 0) || (xPercent > 1) && ((yPercent <= 0) || (yPercent > 1))) {
12         return null;
13     }
14 
15     if (!_isType(xCallback, 'Function') && !_isType(yCallback, 'Function')) {
16         return null;
17     }
18 
19     var lstObj = document.querySelectorAll(selector);
20     if (lstObj.length < 1) {
21         return null;
22     }
23 
24     var obj = lstObj[0];
25     var isProc = {
26         'x': false,
27         'y': false,
28     };
29 
30     obj.onscroll = function(e) {
31         if (isProc.x === false) {
32             if ((0 < xPercent) && (xPercent <= 1)) {
33                 if (((obj.scrollLeft + obj.clientWidth) / obj.scrollWidth) >= xPercent) {
34                     isProc.x = true;
35                     _isType(xCallback, 'Function') && xCallback(function() {
36                         isProc.x = false;
37                     });
38                 }
39             }
40         }
41 
42         if (isProc.y === false) {
43             if ((0 < yPercent) && (yPercent <= 1)) {
44                 if (((obj.scrollTop + obj.clientHeight) / obj.scrollHeight) >= yPercent) {
45                     isProc.y = true;
46                     _isType(yCallback, 'Function') && yCallback(function() {
47                         isProc.y = false;
48                     });
49                 }
50             }
51         }
52     };
53 }
54 
55 procScroll('.img-wrap', .7, function(_done) {
56     setTimeout(function() {
57         _done();
58         alert('x');
59     }, 2000);
60 }, .8, function(_done) {
61     setTimeout(function() {
62         _done();
63         alert('y');
64     }, 2000);
65 });

 

posted @ 2020-06-11 11:47  xlsdg  阅读(103)  评论(0)    收藏  举报