Skip to content

Commit 082b706

Browse files
committed
fix (theme): animation scroll top bug
1 parent 5b598a2 commit 082b706

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

src/js/classes/Animation.js

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import AbstractDomElement from './AbstractDomElement'
2-
import { ScrollObserver, SplittedText } from 'oneloop.js'
2+
import { ScrollObserver, SplittedText, ThrottledEvent } from 'oneloop.js'
33
import noop from '../utils/noop'
44

55
// ----
@@ -8,6 +8,7 @@ import noop from '../utils/noop'
88
const instances = []
99
const animationClass = 'js-animation'
1010
let scrollObserver
11+
let resize
1112

1213
// ----
1314
// class Animation
@@ -33,6 +34,9 @@ class Animation extends AbstractDomElement {
3334
const end = getValue(el, s.end)
3435
const callbacksSharedData = {}
3536

37+
this._elementHeight = this._element.offsetHeight
38+
this._windowHeight = window.innerHeight
39+
this._onResize = onResize.bind(this)
3640
this._isVisible = false
3741
this._callbacksSharedData = callbacksSharedData
3842

@@ -44,11 +48,14 @@ class Animation extends AbstractDomElement {
4448
window.addEventListener('afterprint', onAfterPrint)
4549
}
4650

47-
// init scrollObserver
51+
// init scrollObserver and throttledEvent
4852
if (!scrollObserver) {
4953
scrollObserver = new ScrollObserver()
54+
resize = new ThrottledEvent(window, 'resize')
5055
}
5156

57+
resize.add('resize', this._onResize)
58+
5259
// add animation class
5360
el.classList.add(s.animationClass)
5461

@@ -57,8 +64,11 @@ class Animation extends AbstractDomElement {
5764

5865
// add element to scrollObserver
5966
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) {
6272
// show element
6373
that._isVisible = true
6474
s.onShow(el, scrollInfos, callbacksSharedData)
@@ -68,7 +78,7 @@ class Animation extends AbstractDomElement {
6878
if (s.playOnce) {
6979
that.destroy(el, scrollInfos, callbacksSharedData)
7080
}
71-
} else if (that._isVisible && (percentRTE.y < start || (percentRTE.y > end && s.hideOnReachEnd))) {
81+
} else if (that._isVisible && (pStart < start || (pEnd > end && s.hideOnReachEnd))) {
7282
// hide element
7383
that._isVisible = false
7484
s.onHide(el, scrollInfos, callbacksSharedData)
@@ -112,9 +122,11 @@ class Animation extends AbstractDomElement {
112122
}
113123

114124
scrollObserver.unobserve(el)
125+
resize.remove('resize', this._onResize)
115126

116127
if (!scrollObserver.hasEntry) {
117-
scrollObserver.destroy()
128+
scrollObserver = scrollObserver.destroy()
129+
resize = resize.destroy()
118130
}
119131

120132
if (instances.length === 0) {
@@ -159,7 +171,7 @@ Animation.defaults = {
159171
// if true, the instance will be destroyed after the element is visible
160172
playOnce: false,
161173
// if true, remove the visible class when the element reach the end paramter value
162-
hideOnReachEnd: true,
174+
hideOnReachEnd: false,
163175
// if true, set the element visible on destroy whatever the current scroll value
164176
showOnDestroy: true,
165177
// for each callback : function (element, scrollInfos, callbacksSharedData)
@@ -170,29 +182,18 @@ Animation.defaults = {
170182
}
171183

172184
// ----
173-
// utils
185+
// events
174186
// ----
175187
/**
176-
* get value
177-
* @param {HTMLElement} element
178-
* @param {mixed} value
179-
* @returns {mixed}
188+
* onResize
189+
* @param {Object} event
190+
* @returns {undefined}
180191
*/
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
191195
}
192196

193-
// ----
194-
// events
195-
// ----
196197
/**
197198
* On before print
198199
* @returns {undefined}
@@ -208,6 +209,27 @@ function onAfterPrint() {
208209
document.documentElement.classList.add(animationClass)
209210
}
210211

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+
211233
// ----
212234
// presets
213235
// ----

0 commit comments

Comments
 (0)