@@ -19,7 +19,7 @@ import {
19
19
*/
20
20
function ParallaxController ( ) {
21
21
// All parallax elements to be updated
22
- const elements = [ ] ;
22
+ let elements = [ ] ;
23
23
24
24
// Tracks current scroll y distance
25
25
let scrollY = 0 ;
@@ -323,15 +323,16 @@ function ParallaxController() {
323
323
*/
324
324
this . createElement = function ( options ) {
325
325
const id = _createID ( ) ;
326
- const element = {
326
+ const newElement = {
327
327
id,
328
328
...options ,
329
329
} ;
330
330
331
- elements . push ( element ) ;
331
+ const updatedElements = [ ...elements , newElement ] ;
332
+ elements = updatedElements ;
332
333
this . update ( ) ;
333
334
334
- return element ;
335
+ return newElement ;
335
336
} ;
336
337
337
338
/**
@@ -340,12 +341,8 @@ function ParallaxController() {
340
341
* @param {object } element
341
342
*/
342
343
this . removeElement = function ( element ) {
343
- // gets the index of the element to update based on id
344
- const index = elements . findIndex ( el => el . id === element . id ) ;
345
-
346
- if ( index !== - 1 ) {
347
- elements . splice ( index , 1 ) ;
348
- }
344
+ const updatedElements = elements . filter ( el => el . id !== element . id ) ;
345
+ elements = updatedElements ;
349
346
} ;
350
347
351
348
/**
@@ -354,16 +351,19 @@ function ParallaxController() {
354
351
* @param {object } options
355
352
*/
356
353
this . updateElement = function ( element , options ) {
357
- // gets the index of the element to update based on id
358
- const index = elements . findIndex ( el => el . id === element . id ) ;
354
+ const updatedElements = elements . map ( el => {
355
+ // create element with new options and replaces the old
356
+ if ( el . id === element . id ) {
357
+ // update props
358
+ el . props = options . props ;
359
+ }
360
+ return el ;
361
+ } ) ;
359
362
360
- // create new element with options and replaces the old
361
- if ( index !== - 1 ) {
362
- elements [ index ] = Object . assign ( { } , elements [ index ] , options ) ;
363
+ elements = updatedElements ;
363
364
364
- // call update to set attributes and positions based on the new options
365
- this . update ( ) ;
366
- }
365
+ // call update to set attributes and positions based on the new options
366
+ this . update ( ) ;
367
367
} ;
368
368
369
369
/**
0 commit comments