5
5
*/
6
6
( function ( $ ) {
7
7
var inviewObjects = { } , viewportSize , viewportOffset ,
8
- d = document , w = window , documentElement = d . documentElement , expando = $ . expando ;
8
+ d = document , w = window , documentElement = d . documentElement , expando = $ . expando , timer ;
9
9
10
10
$ . event . special . inview = {
11
11
add : function ( data ) {
12
12
inviewObjects [ data . guid + "-" + this [ expando ] ] = { data : data , $element : $ ( this ) } ;
13
+
14
+ // Use setInterval in order to also make sure this captures elements within
15
+ // "overflow:scroll" elements or elements that appeared in the dom tree due to
16
+ // dom manipulation and reflow
17
+ // old: $(window).scroll(checkInView);
18
+ //
19
+ // By the way, iOS (iPad, iPhone, ...) seems to not execute, or at least delays
20
+ // intervals while the user scrolls. Therefore the inview event might fire a bit late there
21
+ //
22
+ // Don't waste cycles with an interval until we get at least one element that
23
+ // has bound to the inview event.
24
+ if ( ! timer && ! $ . isEmptyObject ( inviewObjects ) ) {
25
+ timer = setInterval ( checkInView , 250 ) ;
26
+ }
13
27
} ,
14
28
15
29
remove : function ( data ) {
16
30
try { delete inviewObjects [ data . guid + "-" + this [ expando ] ] ; } catch ( e ) { }
31
+
32
+ // Clear interval when we no longer have any elements listening
33
+ if ( $ . isEmptyObject ( inviewObjects ) ) {
34
+ clearInterval ( timer ) ;
35
+ timer = null ;
36
+ }
17
37
}
18
38
} ;
19
39
113
133
viewportOffset = null ;
114
134
} ) ;
115
135
}
116
-
117
- // Use setInterval in order to also make sure this captures elements within
118
- // "overflow:scroll" elements or elements that appeared in the dom tree due to
119
- // dom manipulation and reflow
120
- // old: $(window).scroll(checkInView);
121
- //
122
- // By the way, iOS (iPad, iPhone, ...) seems to not execute, or at least delays
123
- // intervals while the user scrolls. Therefore the inview event might fire a bit late there
124
- setInterval ( checkInView , 250 ) ;
125
136
} ) ( jQuery ) ;
0 commit comments