forked from MarkBiesheuvel/optimizely-custom-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll-to-element.js
30 lines (24 loc) · 909 Bytes
/
scroll-to-element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Using the "When a callback is called" page activation trigger
// See https://docs.developers.optimizely.com/web/docs/dynamic-websites#section-callback
function callbackFn(activate) {
// Get Optimizely Utilities library
const utils = window.optimizely.get('utils');
// Wait/listen for a given selector
utils.waitForElement('.product-sizes__product-size-container').then((elem) => {
// Create a observer for the entire viewport
const observer = new IntersectionObserver((entries, observer) => {
// Iterate over entries
entries.forEach((entry) => {
// If entry is visible ...
if (entry.isIntersecting) {
// Activate experiment
activate();
// Deactivate observer
observer.disconnect();
}
});
});
// Only activate the page/experiment once the container is visible
observer.observe(elem);
});
}