File tree Expand file tree Collapse file tree 3 files changed +49
-27
lines changed Expand file tree Collapse file tree 3 files changed +49
-27
lines changed Original file line number Diff line number Diff line change 12
12
this . $el = $ ( el ) ;
13
13
this . cb = cb ;
14
14
this . offset = offset ;
15
+ this . previousIsInState = false ;
16
+
17
+ // Make the first check
18
+ this . check ( ) ;
19
+
20
+ // Start watching.
15
21
this . watch ( ) ;
22
+
16
23
return this ;
17
24
} ;
18
25
23
30
*
24
31
* @returns {boolean }
25
32
*/
26
- isIn : function isIn ( ) {
33
+ isIn : function ( ) {
27
34
const _self = this ;
28
35
const $win = $ ( window ) ;
29
36
const elementTop = _self . $el . offset ( ) . top - _self . offset ;
38
45
*/
39
46
watch : function ( ) {
40
47
const self = this ;
41
- let isIn = false ;
48
+ $ ( window ) . on ( 'resize scroll' , self . check . bind ( self ) ) ;
49
+ } ,
42
50
43
- $ ( window ) . on ( 'resize scroll' , function ( ) {
51
+ /**
52
+ * Checks if the element is on in the viewport.
53
+ */
54
+ check : function ( ) {
55
+ const self = this ;
44
56
45
- if ( self . isIn ( ) && isIn === false ) {
46
- self . cb . call ( self . $el , 'entered' ) ;
47
- isIn = true ;
48
- }
57
+ if ( self . isIn ( ) && self . previousIsInState === false ) {
58
+ self . cb . call ( self . $el , 'entered' ) ;
59
+ self . previousIsInState = true ;
60
+ }
49
61
50
- if ( isIn === true && ! self . isIn ( ) ) {
51
- self . cb . call ( self . $el , 'leaved' ) ;
52
- isIn = false ;
53
- }
54
- } )
62
+ if ( self . previousIsInState === true && ! self . isIn ( ) ) {
63
+ self . cb . call ( self . $el , 'leaved' ) ;
64
+ self . previousIsInState = false ;
65
+ }
55
66
}
56
67
} ;
57
68
69
80
} )
70
81
}
71
82
72
- } ( window . jQuery ) ;
83
+ } ( window . jQuery ) ;
Original file line number Diff line number Diff line change 12
12
this . $el = $ ( el ) ;
13
13
this . cb = cb ;
14
14
this . offset = offset ;
15
+ this . previousIsInState = false ;
16
+
17
+ // Make the first check
18
+ this . check ( ) ;
19
+
20
+ // Start watching.
15
21
this . watch ( ) ;
22
+
16
23
return this ;
17
24
} ;
18
25
23
30
*
24
31
* @returns {boolean }
25
32
*/
26
- isIn : function isIn ( ) {
33
+ isIn : function ( ) {
27
34
const _self = this ;
28
35
const $win = $ ( window ) ;
29
36
const elementTop = _self . $el . offset ( ) . top - _self . offset ;
38
45
*/
39
46
watch : function ( ) {
40
47
const self = this ;
41
- let isIn = false ;
48
+ $ ( window ) . on ( 'resize scroll' , self . check . bind ( self ) ) ;
49
+ } ,
42
50
43
- $ ( window ) . on ( 'resize scroll' , function ( ) {
51
+ /**
52
+ * Checks if the element is on in the viewport.
53
+ */
54
+ check : function ( ) {
55
+ const self = this ;
44
56
45
- if ( self . isIn ( ) && isIn === false ) {
46
- self . cb . call ( self . $el , 'entered' ) ;
47
- isIn = true ;
48
- }
57
+ if ( self . isIn ( ) && self . previousIsInState === false ) {
58
+ self . cb . call ( self . $el , 'entered' ) ;
59
+ self . previousIsInState = true ;
60
+ }
49
61
50
- if ( isIn === true && ! self . isIn ( ) ) {
51
- self . cb . call ( self . $el , 'leaved' ) ;
52
- isIn = false ;
53
- }
54
- } )
62
+ if ( self . previousIsInState === true && ! self . isIn ( ) ) {
63
+ self . cb . call ( self . $el , 'leaved' ) ;
64
+ self . previousIsInState = false ;
65
+ }
55
66
}
56
67
} ;
57
68
69
80
} )
70
81
}
71
82
72
- } ( window . jQuery ) ;
83
+ } ( window . jQuery ) ;
You can’t perform that action at this time.
0 commit comments