Skip to content

Commit 66befe6

Browse files
committed
Fix initialization, refresh on window load
1 parent ec1cd60 commit 66befe6

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

src/js/aos.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,32 @@ let options = {
4444
useClassNames: false
4545
};
4646

47+
const initializeScroll = function initializeScroll() {
48+
// Extend elements objects in $aosElements with their positions
49+
$aosElements = prepare($aosElements, options);
50+
// Perform scroll event, to refresh view and show/hide elements
51+
handleScroll($aosElements);
52+
53+
/**
54+
* Handle scroll event to animate elements on scroll
55+
*/
56+
window.addEventListener(
57+
'scroll',
58+
throttle(() => {
59+
handleScroll($aosElements, options.once);
60+
}, 99)
61+
);
62+
63+
return $aosElements;
64+
};
65+
4766
/**
4867
* Refresh AOS
4968
*/
5069
const refresh = function refresh(initialize = false) {
5170
// Allow refresh only when it was first initialized on startEvent
5271
if (initialize) initialized = true;
53-
54-
if (initialized) {
55-
// Extend elements objects in $aosElements with their positions
56-
$aosElements = prepare($aosElements, options);
57-
// Perform scroll event, to refresh view and show/hide elements
58-
handleScroll($aosElements);
59-
60-
return $aosElements;
61-
}
72+
if (initialized) initializeScroll();
6273
};
6374

6475
/**
@@ -144,22 +155,23 @@ const init = function init(settings) {
144155
/**
145156
* Handle initializing
146157
*/
158+
if (['DOMContentLoaded', 'load'].indexOf(options.startEvent) === -1) {
159+
// Listen to options.startEvent and initialize AOS
160+
document.addEventListener(options.startEvent, function() {
161+
refresh(true);
162+
});
163+
} else {
164+
window.addEventListener('load', function() {
165+
refresh(true);
166+
});
167+
}
168+
147169
if (
148170
options.startEvent === 'DOMContentLoaded' &&
149171
['complete', 'interactive'].indexOf(document.readyState) > -1
150172
) {
151173
// Initialize AOS if default startEvent was already fired
152174
refresh(true);
153-
} else if (options.startEvent === 'load') {
154-
// If start event is 'Load' - attach listener to window
155-
window.addEventListener(options.startEvent, function() {
156-
refresh(true);
157-
});
158-
} else {
159-
// Listen to options.startEvent and initialize AOS
160-
document.addEventListener(options.startEvent, function() {
161-
refresh(true);
162-
});
163175
}
164176

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

171-
/**
172-
* Handle scroll event to animate elements on scroll
173-
*/
174-
window.addEventListener(
175-
'scroll',
176-
throttle(() => {
177-
handleScroll($aosElements, options.once);
178-
}, 99)
179-
);
180-
181183
/**
182184
* Observe [aos] elements
183185
* If something is loaded by AJAX

0 commit comments

Comments
 (0)