Skip to content

Commit

Permalink
Fix AOS detection in MutationObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsnik committed Jul 8, 2018
1 parent 835ad10 commit 68a0d52
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/js/libs/observer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
let callback = () => {};

function containsAOSNode(nodes) {
let i, currentNode, result;

for (i = 0; i < nodes.length; i += 1) {
currentNode = nodes[i];

if (currentNode.dataset && currentNode.dataset.aos) {
return true;
}

result = currentNode.children && containsAOSNode(currentNode.children);

if (result) {
return true;
}
}

return false;
}

function ready(selector, fn) {
const doc = window.document;
const MutationObserver =
Expand All @@ -23,13 +43,10 @@ function check(mutations) {
mutations.forEach(mutation => {
const addedNodes = Array.prototype.slice.call(mutation.addedNodes);
const removedNodes = Array.prototype.slice.call(mutation.removedNodes);
const allNodes = addedNodes.concat(removedNodes);

const anyAOSElementAdded = addedNodes
.concat(removedNodes)
.filter(el => el.hasAttribute && el.hasAttribute('data-aos')).length;

if (anyAOSElementAdded) {
callback();
if (containsAOSNode(allNodes)) {
return callback();
}
});
}
Expand Down

0 comments on commit 68a0d52

Please sign in to comment.