@@ -15,6 +15,17 @@ export default class SimpleParallax {
15
15
16
16
// check if the browser support simpleParallax
17
17
if ( ! isSupportedBrowser ( ) ) return ;
18
+
19
+ // Check if user prefers reduced motion
20
+ this . prefersReducedMotion = window . matchMedia ( '(prefers-reduced-motion: reduce)' ) . matches ;
21
+
22
+ // Set up listener for changes to reduced motion preference
23
+ this . reducedMotionMediaQuery = window . matchMedia ( '(prefers-reduced-motion: reduce)' ) ;
24
+ this . handleReducedMotionChange = this . handleReducedMotionChange . bind ( this ) ;
25
+ this . reducedMotionMediaQuery . addEventListener ( 'change' , this . handleReducedMotionChange ) ;
26
+
27
+ // If reduced motion is preferred, don't initialize parallax effects
28
+ if ( this . prefersReducedMotion ) return ;
18
29
19
30
this . elements = convertToArray ( elements ) ;
20
31
this . defaults = {
@@ -45,20 +56,33 @@ export default class SimpleParallax {
45
56
46
57
this . init ( ) ;
47
58
}
59
+
60
+ // Handle changes to reduced motion preference
61
+ handleReducedMotionChange ( event ) {
62
+ this . prefersReducedMotion = event . matches ;
63
+
64
+ if ( this . prefersReducedMotion ) {
65
+ // If user now prefers reduced motion, remove all parallax effects
66
+ this . destroy ( ) ;
67
+ } else {
68
+ // If user no longer prefers reduced motion, reinitialize
69
+ this . init ( ) ;
70
+ }
71
+ }
48
72
49
73
init ( ) {
74
+ // Don't initialize if reduced motion is preferred
75
+ if ( this . prefersReducedMotion ) return ;
76
+
50
77
viewport . setViewportAll ( this . customContainer ) ;
51
78
52
79
instances = [
53
80
...this . elements . map (
54
- ( element ) => new ParallaxInstance ( element , this . settings )
81
+ ( element ) => new ParallaxInstance ( element , this . settings , this . prefersReducedMotion )
55
82
) ,
56
83
...instances
57
84
] ;
58
85
59
- // update the instance length
60
- // instancesLength = instances.length;
61
-
62
86
// only if this is the first simpleParallax init
63
87
if ( ! isInit ) {
64
88
// init the frame
@@ -109,7 +133,7 @@ export default class SimpleParallax {
109
133
}
110
134
111
135
// proceed the element
112
- proceedElement ( instance ) {
136
+ proceedElement ( instance ) {
113
137
let isVisible = false ;
114
138
115
139
// if this is a custom container
@@ -150,11 +174,16 @@ export default class SimpleParallax {
150
174
}
151
175
152
176
destroy ( ) {
177
+ // Remove reduced motion event listener
178
+ if ( this . reducedMotionMediaQuery ) {
179
+ this . reducedMotionMediaQuery . removeEventListener ( 'change' , this . handleReducedMotionChange ) ;
180
+ }
181
+
153
182
const instancesToDestroy = [ ] ;
154
183
155
184
// remove all instances that need to be destroyed from the instances array
156
185
instances = instances . filter ( ( instance ) => {
157
- if ( this . elements . includes ( instance . element ) ) {
186
+ if ( this . elements && this . elements . includes ( instance . element ) ) {
158
187
// push instance that need to be destroyed into instancesToDestroy
159
188
instancesToDestroy . push ( instance ) ;
160
189
return false ;
@@ -166,7 +195,7 @@ export default class SimpleParallax {
166
195
// unset style
167
196
instance . unSetStyle ( ) ;
168
197
169
- if ( this . settings . overflow === false ) {
198
+ if ( this . settings && this . settings . overflow === false ) {
170
199
// if overflow option is set to false
171
200
// unwrap the element from .simpleParallax wrapper container
172
201
instance . unWrapElement ( ) ;
0 commit comments