1
1
import AbstractDomElement from './AbstractDomElement'
2
- import { ScrollObserver , SplittedText } from 'oneloop.js'
2
+ import { ScrollObserver , SplittedText , ThrottledEvent } from 'oneloop.js'
3
3
import noop from '../utils/noop'
4
4
5
5
// ----
@@ -8,6 +8,7 @@ import noop from '../utils/noop'
8
8
const instances = [ ]
9
9
const animationClass = 'js-animation'
10
10
let scrollObserver
11
+ let resize
11
12
12
13
// ----
13
14
// class Animation
@@ -33,6 +34,9 @@ class Animation extends AbstractDomElement {
33
34
const end = getValue ( el , s . end )
34
35
const callbacksSharedData = { }
35
36
37
+ this . _elementHeight = this . _element . offsetHeight
38
+ this . _windowHeight = window . innerHeight
39
+ this . _onResize = onResize . bind ( this )
36
40
this . _isVisible = false
37
41
this . _callbacksSharedData = callbacksSharedData
38
42
@@ -44,11 +48,14 @@ class Animation extends AbstractDomElement {
44
48
window . addEventListener ( 'afterprint' , onAfterPrint )
45
49
}
46
50
47
- // init scrollObserver
51
+ // init scrollObserver and throttledEvent
48
52
if ( ! scrollObserver ) {
49
53
scrollObserver = new ScrollObserver ( )
54
+ resize = new ThrottledEvent ( window , 'resize' )
50
55
}
51
56
57
+ resize . add ( 'resize' , this . _onResize )
58
+
52
59
// add animation class
53
60
el . classList . add ( s . animationClass )
54
61
@@ -57,8 +64,11 @@ class Animation extends AbstractDomElement {
57
64
58
65
// add element to scrollObserver
59
66
scrollObserver . observe ( el , {
60
- onVisible : function ( scrollInfos , percentRTW , percentRTE ) {
61
- if ( ! that . _isVisible && percentRTE . y >= start && percentRTE . y <= end ) {
67
+ onVisible : function ( scrollInfos , percentRTW ) {
68
+ const pStart = ( this . distanceRTW . y * percentRTW . y ) / that . _windowHeight
69
+ const pEnd = ( this . distanceRTW . y * percentRTW . y ) / ( that . _windowHeight + that . _elementHeight )
70
+
71
+ if ( ! that . _isVisible && pStart >= start && pEnd <= end ) {
62
72
// show element
63
73
that . _isVisible = true
64
74
s . onShow ( el , scrollInfos , callbacksSharedData )
@@ -68,7 +78,7 @@ class Animation extends AbstractDomElement {
68
78
if ( s . playOnce ) {
69
79
that . destroy ( el , scrollInfos , callbacksSharedData )
70
80
}
71
- } else if ( that . _isVisible && ( percentRTE . y < start || ( percentRTE . y > end && s . hideOnReachEnd ) ) ) {
81
+ } else if ( that . _isVisible && ( pStart < start || ( pEnd > end && s . hideOnReachEnd ) ) ) {
72
82
// hide element
73
83
that . _isVisible = false
74
84
s . onHide ( el , scrollInfos , callbacksSharedData )
@@ -112,9 +122,11 @@ class Animation extends AbstractDomElement {
112
122
}
113
123
114
124
scrollObserver . unobserve ( el )
125
+ resize . remove ( 'resize' , this . _onResize )
115
126
116
127
if ( ! scrollObserver . hasEntry ) {
117
- scrollObserver . destroy ( )
128
+ scrollObserver = scrollObserver . destroy ( )
129
+ resize = resize . destroy ( )
118
130
}
119
131
120
132
if ( instances . length === 0 ) {
@@ -159,7 +171,7 @@ Animation.defaults = {
159
171
// if true, the instance will be destroyed after the element is visible
160
172
playOnce : false ,
161
173
// if true, remove the visible class when the element reach the end paramter value
162
- hideOnReachEnd : true ,
174
+ hideOnReachEnd : false ,
163
175
// if true, set the element visible on destroy whatever the current scroll value
164
176
showOnDestroy : true ,
165
177
// for each callback : function (element, scrollInfos, callbacksSharedData)
@@ -170,29 +182,18 @@ Animation.defaults = {
170
182
}
171
183
172
184
// ----
173
- // utils
185
+ // events
174
186
// ----
175
187
/**
176
- * get value
177
- * @param {HTMLElement } element
178
- * @param {mixed } value
179
- * @returns {mixed }
188
+ * onResize
189
+ * @param {Object } event
190
+ * @returns {undefined }
180
191
*/
181
- function getValue ( element , value ) {
182
- let rt = value
183
-
184
- if ( typeof value === 'function' ) {
185
- rt = value ( element )
186
- } else if ( Array . isArray ( value ) ) {
187
- rt = Math . random ( ) * ( value [ 1 ] - value [ 0 ] ) + value [ 0 ]
188
- }
189
-
190
- return rt
192
+ function onResize ( ) {
193
+ this . _elementHeight = this . _element . offsetHeight
194
+ this . _windowHeight = window . innerHeight
191
195
}
192
196
193
- // ----
194
- // events
195
- // ----
196
197
/**
197
198
* On before print
198
199
* @returns {undefined }
@@ -208,6 +209,27 @@ function onAfterPrint() {
208
209
document . documentElement . classList . add ( animationClass )
209
210
}
210
211
212
+ // ----
213
+ // utils
214
+ // ----
215
+ /**
216
+ * get value
217
+ * @param {HTMLElement } element
218
+ * @param {mixed } value
219
+ * @returns {mixed }
220
+ */
221
+ function getValue ( element , value ) {
222
+ let rt = value
223
+
224
+ if ( typeof value === 'function' ) {
225
+ rt = value ( element )
226
+ } else if ( Array . isArray ( value ) ) {
227
+ rt = Math . random ( ) * ( value [ 1 ] - value [ 0 ] ) + value [ 0 ]
228
+ }
229
+
230
+ return rt
231
+ }
232
+
211
233
// ----
212
234
// presets
213
235
// ----
0 commit comments