Skip to content

Commit

Permalink
Merge pull request michalsnik#24 from michalsnik/fix-refresh-on-ajax
Browse files Browse the repository at this point in the history
Fix refresh on DOM change
  • Loading branch information
Michał Sajnóg committed Jun 18, 2016
2 parents b99bd82 + fb0626d commit 9bc42b8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
43 changes: 43 additions & 0 deletions demo/async.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AOS - Animate on scroll library</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/styles.css" />
<link rel="stylesheet" href="../dist/aos.css" />
</head>
<body>
<div id="aos-demo" class="aos-all">
<div class="aos-item" aos="fade-up">
<div class="aos-item__inner colored-1"><h3>Lorem ipsum</h3></div>
</div>
<div class="aos-item" aos="fade-down">
<div class="aos-item__inner colored-2"><h3>dolor sit amet</h3></div>
</div>
<div class="aos-item" aos="zoom-out-down">
<div class="aos-item__inner colored-3"><h3>onsectetur adipisicing</h3></div>
</div>
</div>

<script src="../dist/aos.js"></script>
<script>
AOS.init({
easing: 'ease-in-out-sine'
});

setInterval(addItem, 1000);

var itemsCounter = 4;
var container = document.getElementById('aos-demo');

function addItem () {
var item = document.createElement('div');
item.classList.add('aos-item');
item.setAttribute('aos', 'fade-up');
item.innerHTML = '<div class="aos-item__inner colored-' + itemsCounter + '"><h3>Lorem ipsum</h3></div>';
container.appendChild(item);
}
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion dist/aos.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/aos.js.map

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions src/js/aos.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ var replaceDataAttr = require('./helpers/replaceDataAttr');
}
};

/**
* Hard refresh
* create array with new elements and trigger refresh
*/
var refreshHard = function() {
$aosElements = elements();
refresh();
};

/**
* Initializing AOS
* - Create options merging defaults with user defined options
Expand Down Expand Up @@ -142,7 +151,7 @@ var replaceDataAttr = require('./helpers/replaceDataAttr');
document.addEventListener('DOMNodeRemoved', function(event) {
var el = event.target;
if (el && el.nodeType === 1 && el.hasAttribute && event.target.hasAttribute('aos')) {
_debounce(refresh, 50, true)
_debounce(refreshHard, 50, true)
}
});

Expand All @@ -151,7 +160,7 @@ var replaceDataAttr = require('./helpers/replaceDataAttr');
* If something is loaded by AJAX
* it'll refresh plugin automatically
*/
observe('[aos]', refresh);
observe('[aos]', refreshHard);

return $aosElements;
};
Expand All @@ -161,7 +170,8 @@ var replaceDataAttr = require('./helpers/replaceDataAttr');
*/
var AOS = {
init: init,
refresh: refresh
refresh: refresh,
refreshHard: refreshHard
};

module.exports = AOS;
Expand Down

0 comments on commit 9bc42b8

Please sign in to comment.