@@ -19,7 +19,7 @@ import {
1919 */
2020function ParallaxController ( ) {
2121 // All parallax elements to be updated
22- const elements = [ ] ;
22+ let elements = [ ] ;
2323
2424 // Tracks current scroll y distance
2525 let scrollY = 0 ;
@@ -323,15 +323,16 @@ function ParallaxController() {
323323 */
324324 this . createElement = function ( options ) {
325325 const id = _createID ( ) ;
326- const element = {
326+ const newElement = {
327327 id,
328328 ...options ,
329329 } ;
330330
331- elements . push ( element ) ;
331+ const updatedElements = [ ...elements , newElement ] ;
332+ elements = updatedElements ;
332333 this . update ( ) ;
333334
334- return element ;
335+ return newElement ;
335336 } ;
336337
337338 /**
@@ -340,12 +341,8 @@ function ParallaxController() {
340341 * @param {object } element
341342 */
342343 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 ;
349346 } ;
350347
351348 /**
@@ -354,16 +351,19 @@ function ParallaxController() {
354351 * @param {object } options
355352 */
356353 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+ } ) ;
359362
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 ;
363364
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 ( ) ;
367367 } ;
368368
369369 /**
0 commit comments