Skip to content

Commit

Permalink
Fix initialization, refresh on window load
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsnik committed Jun 20, 2018
1 parent ec1cd60 commit 66befe6
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions src/js/aos.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,32 @@ let options = {
useClassNames: false
};

const initializeScroll = function initializeScroll() {
// Extend elements objects in $aosElements with their positions
$aosElements = prepare($aosElements, options);
// Perform scroll event, to refresh view and show/hide elements
handleScroll($aosElements);

/**
* Handle scroll event to animate elements on scroll
*/
window.addEventListener(
'scroll',
throttle(() => {
handleScroll($aosElements, options.once);
}, 99)
);

return $aosElements;
};

/**
* Refresh AOS
*/
const refresh = function refresh(initialize = false) {
// Allow refresh only when it was first initialized on startEvent
if (initialize) initialized = true;

if (initialized) {
// Extend elements objects in $aosElements with their positions
$aosElements = prepare($aosElements, options);
// Perform scroll event, to refresh view and show/hide elements
handleScroll($aosElements);

return $aosElements;
}
if (initialized) initializeScroll();
};

/**
Expand Down Expand Up @@ -144,22 +155,23 @@ const init = function init(settings) {
/**
* Handle initializing
*/
if (['DOMContentLoaded', 'load'].indexOf(options.startEvent) === -1) {
// Listen to options.startEvent and initialize AOS
document.addEventListener(options.startEvent, function() {
refresh(true);
});
} else {
window.addEventListener('load', function() {
refresh(true);
});
}

if (
options.startEvent === 'DOMContentLoaded' &&
['complete', 'interactive'].indexOf(document.readyState) > -1
) {
// Initialize AOS if default startEvent was already fired
refresh(true);
} else if (options.startEvent === 'load') {
// If start event is 'Load' - attach listener to window
window.addEventListener(options.startEvent, function() {
refresh(true);
});
} else {
// Listen to options.startEvent and initialize AOS
document.addEventListener(options.startEvent, function() {
refresh(true);
});
}

/**
Expand All @@ -168,16 +180,6 @@ const init = function init(settings) {
window.addEventListener('resize', debounce(refresh, 50, true));
window.addEventListener('orientationchange', debounce(refresh, 50, true));

/**
* Handle scroll event to animate elements on scroll
*/
window.addEventListener(
'scroll',
throttle(() => {
handleScroll($aosElements, options.once);
}, 99)
);

/**
* Observe [aos] elements
* If something is loaded by AJAX
Expand Down

0 comments on commit 66befe6

Please sign in to comment.