Skip to content

Commit 0e974d1

Browse files
authored
Merge pull request #376 from BeAPI/fix/animation-js
Fix/animation js
2 parents d82878b + 9299eeb commit 0e974d1

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

src/js/classes/Animation.js

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ import noop from '../utils/noop'
66
// shared variables
77
// ----
88
const instances = []
9+
const animationClass = 'js-animation'
910
let scrollObserver
1011

1112
// ----
1213
// class Animation
1314
// ----
1415
class Animation extends AbstractDomElement {
16+
/**
17+
* Animation constructor
18+
* @param {HTMLElement} element
19+
* @param {object} options
20+
*/
1521
constructor(element, options) {
1622
const instance = super(element, options)
1723

@@ -33,6 +39,11 @@ class Animation extends AbstractDomElement {
3339
// add to instances
3440
instances.push(this)
3541

42+
if (instances.length === 1) {
43+
window.addEventListener('beforeprint', onBeforePrint)
44+
window.addEventListener('afterprint', onAfterPrint)
45+
}
46+
3647
// init scrollObserver
3748
if (!scrollObserver) {
3849
scrollObserver = new ScrollObserver()
@@ -47,7 +58,7 @@ class Animation extends AbstractDomElement {
4758
// add element to scrollObserver
4859
scrollObserver.observe(el, {
4960
onVisible: function (scrollInfos, percentRTW, percentRTE) {
50-
if (percentRTE.y >= start && percentRTE.y <= end && !that._isVisible) {
61+
if (!that._isVisible && percentRTE.y >= start && percentRTE.y <= end) {
5162
// show element
5263
that._isVisible = true
5364
s.onShow(el, scrollInfos, callbacksSharedData)
@@ -57,7 +68,7 @@ class Animation extends AbstractDomElement {
5768
if (s.playOnce) {
5869
that.destroy(el, scrollInfos, callbacksSharedData)
5970
}
60-
} else if ((percentRTE.y < start || (percentRTE.y > end && s.hideOnReachEnd)) && that._isVisible) {
71+
} else if (that._isVisible && (percentRTE.y < start || (percentRTE.y > end && s.hideOnReachEnd))) {
6172
// hide element
6273
that._isVisible = false
6374
s.onHide(el, scrollInfos, callbacksSharedData)
@@ -67,10 +78,18 @@ class Animation extends AbstractDomElement {
6778
})
6879
}
6980

81+
/**
82+
* Is visible
83+
* @returns {boolean}
84+
*/
7085
isVisible() {
7186
return this._isVisible
7287
}
7388

89+
/**
90+
* Destroy
91+
* @returns {undefined}
92+
*/
7493
destroy() {
7594
const el = this._element
7695
const s = this._settings
@@ -98,14 +117,31 @@ class Animation extends AbstractDomElement {
98117
scrollObserver.destroy()
99118
}
100119

120+
if (instances.length === 0) {
121+
window.removeEventListener('beforeprint', onBeforePrint)
122+
window.removeEventListener('afterprint', onAfterPrint)
123+
}
124+
101125
this._settings.onDestroy(el, scrollInfos, callbacksSharedData)
102126
}
103127

128+
/**
129+
* Static destroy
130+
* @returns {undefined}
131+
*/
104132
static destroy() {
105133
while (instances.length) {
106134
instances[0].destroy()
107135
}
108136
}
137+
138+
/**
139+
* Static are animations enabled
140+
* @returns {boolean}
141+
*/
142+
static areAnimationsEnbaled() {
143+
return document.documentElement.classList.contains(animationClass)
144+
}
109145
}
110146

111147
// ----
@@ -123,7 +159,7 @@ Animation.defaults = {
123159
// if true, the instance will be destroyed after the element is visible
124160
playOnce: false,
125161
// if true, remove the visible class when the element reach the end paramter value
126-
hideOnReachEnd: false,
162+
hideOnReachEnd: true,
127163
// if true, set the element visible on destroy whatever the current scroll value
128164
showOnDestroy: true,
129165
// for each callback : function (element, scrollInfos, callbacksSharedData)
@@ -136,6 +172,12 @@ Animation.defaults = {
136172
// ----
137173
// utils
138174
// ----
175+
/**
176+
* get value
177+
* @param {HTMLElement} element
178+
* @param {mixed} value
179+
* @returns {mixed}
180+
*/
139181
function getValue(element, value) {
140182
let rt = value
141183

@@ -148,6 +190,24 @@ function getValue(element, value) {
148190
return rt
149191
}
150192

193+
// ----
194+
// events
195+
// ----
196+
/**
197+
* On before print
198+
* @returns {undefined}
199+
*/
200+
function onBeforePrint() {
201+
document.documentElement.classList.remove(animationClass)
202+
}
203+
/**
204+
* On after print
205+
* @returns {undefined}
206+
*/
207+
function onAfterPrint() {
208+
document.documentElement.classList.add(animationClass)
209+
}
210+
151211
// ----
152212
// presets
153213
// ----
@@ -179,7 +239,7 @@ Animation.preset = {
179239
},
180240
})
181241

182-
const children = el.children
242+
const children = el.getElementsByClassName('st-line')
183243
const length = children.length
184244
let i
185245

0 commit comments

Comments
 (0)