Skip to content

Commit

Permalink
♻️ refactoring destroy method
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey Signorato committed Jun 4, 2019
1 parent 63ed066 commit 63c5520
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/simpleParallax.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,25 @@ export default class SimpleParallax {
destroy() {
let instancesToDestroy = [];

//get all instance indexes that need to be destroyed
for (let i = instancesLength - 1; i >= 0; i--) {
for (let j = this.elements.length - 1; j >= 0; j--) {
if (instances[i].element === this.elements[j]) {
instancesToDestroy.push(i);
break;
}
//remove all instances that need to be destroyed from the instances array
instances = instances.filter(instance => {
if (this.elements.includes(instance.element)) {
//push instance that need to be destroyed into instancesToDestroy
instancesToDestroy.push(instance);
} else {
return instance;
}
}
});

for (let i = 0; i < instancesToDestroy.length; i++) {
let instanceToDestroy = instancesToDestroy[i];

//remove all style added from simpleParallax
instances[instanceToDestroy].unSetStyle();
for (let instance of instancesToDestroy) {
//unset style
instance.unSetStyle();

if (this.settings.overflow === false) {
//if overflow option is set to false
//unwrap the element from .simpleParallax wrapper container
instances[instanceToDestroy].unWrapElement();
instance.unWrapElement();
}

//remove the instance to destroy from the instance array
instances = instances.slice(0, instanceToDestroy).concat(instances.slice(instanceToDestroy + 1, instances.length));
}

//update the instance length var
Expand Down

0 comments on commit 63c5520

Please sign in to comment.