@@ -6,12 +6,18 @@ import noop from '../utils/noop'
6
6
// shared variables
7
7
// ----
8
8
const instances = [ ]
9
+ const animationClass = 'js-animation'
9
10
let scrollObserver
10
11
11
12
// ----
12
13
// class Animation
13
14
// ----
14
15
class Animation extends AbstractDomElement {
16
+ /**
17
+ * Animation constructor
18
+ * @param {HTMLElement } element
19
+ * @param {object } options
20
+ */
15
21
constructor ( element , options ) {
16
22
const instance = super ( element , options )
17
23
@@ -33,6 +39,11 @@ class Animation extends AbstractDomElement {
33
39
// add to instances
34
40
instances . push ( this )
35
41
42
+ if ( instances . length === 1 ) {
43
+ window . addEventListener ( 'beforeprint' , onBeforePrint )
44
+ window . addEventListener ( 'afterprint' , onAfterPrint )
45
+ }
46
+
36
47
// init scrollObserver
37
48
if ( ! scrollObserver ) {
38
49
scrollObserver = new ScrollObserver ( )
@@ -47,7 +58,7 @@ class Animation extends AbstractDomElement {
47
58
// add element to scrollObserver
48
59
scrollObserver . observe ( el , {
49
60
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 ) {
51
62
// show element
52
63
that . _isVisible = true
53
64
s . onShow ( el , scrollInfos , callbacksSharedData )
@@ -57,7 +68,7 @@ class Animation extends AbstractDomElement {
57
68
if ( s . playOnce ) {
58
69
that . destroy ( el , scrollInfos , callbacksSharedData )
59
70
}
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 ) ) ) {
61
72
// hide element
62
73
that . _isVisible = false
63
74
s . onHide ( el , scrollInfos , callbacksSharedData )
@@ -67,10 +78,18 @@ class Animation extends AbstractDomElement {
67
78
} )
68
79
}
69
80
81
+ /**
82
+ * Is visible
83
+ * @returns {boolean }
84
+ */
70
85
isVisible ( ) {
71
86
return this . _isVisible
72
87
}
73
88
89
+ /**
90
+ * Destroy
91
+ * @returns {undefined }
92
+ */
74
93
destroy ( ) {
75
94
const el = this . _element
76
95
const s = this . _settings
@@ -98,14 +117,31 @@ class Animation extends AbstractDomElement {
98
117
scrollObserver . destroy ( )
99
118
}
100
119
120
+ if ( instances . length === 0 ) {
121
+ window . removeEventListener ( 'beforeprint' , onBeforePrint )
122
+ window . removeEventListener ( 'afterprint' , onAfterPrint )
123
+ }
124
+
101
125
this . _settings . onDestroy ( el , scrollInfos , callbacksSharedData )
102
126
}
103
127
128
+ /**
129
+ * Static destroy
130
+ * @returns {undefined }
131
+ */
104
132
static destroy ( ) {
105
133
while ( instances . length ) {
106
134
instances [ 0 ] . destroy ( )
107
135
}
108
136
}
137
+
138
+ /**
139
+ * Static are animations enabled
140
+ * @returns {boolean }
141
+ */
142
+ static areAnimationsEnbaled ( ) {
143
+ return document . documentElement . classList . contains ( animationClass )
144
+ }
109
145
}
110
146
111
147
// ----
@@ -123,7 +159,7 @@ Animation.defaults = {
123
159
// if true, the instance will be destroyed after the element is visible
124
160
playOnce : false ,
125
161
// if true, remove the visible class when the element reach the end paramter value
126
- hideOnReachEnd : false ,
162
+ hideOnReachEnd : true ,
127
163
// if true, set the element visible on destroy whatever the current scroll value
128
164
showOnDestroy : true ,
129
165
// for each callback : function (element, scrollInfos, callbacksSharedData)
@@ -136,6 +172,12 @@ Animation.defaults = {
136
172
// ----
137
173
// utils
138
174
// ----
175
+ /**
176
+ * get value
177
+ * @param {HTMLElement } element
178
+ * @param {mixed } value
179
+ * @returns {mixed }
180
+ */
139
181
function getValue ( element , value ) {
140
182
let rt = value
141
183
@@ -148,6 +190,24 @@ function getValue(element, value) {
148
190
return rt
149
191
}
150
192
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
+
151
211
// ----
152
212
// presets
153
213
// ----
@@ -179,7 +239,7 @@ Animation.preset = {
179
239
} ,
180
240
} )
181
241
182
- const children = el . children
242
+ const children = el . getElementsByClassName ( 'st-line' )
183
243
const length = children . length
184
244
let i
185
245
0 commit comments