jQuery(document).ready(function($) {
// create new observer with the trigger point when the element is 25% visible
let observer = new IntersectionObserver( isElScrolledIntoView, {
root: null,
rootMargin: '0px',
threshold: 0.25
});
// grab elements by global class
const elements = document.querySelectorAll('.product-section');
// loop over all elements and observe each of them
elements.forEach( element => {
observer.observe(element);
});
// create function to perform action on each observed element
function isElScrolledIntoView( entries ) {
// again loop over all entries (element)
entries.forEach( entry => {
// check if the entry is intersecting at our set threshold
if ( entry.isIntersecting ) {
// set a class to toggle animation
entry.target.classList.add('view-animation');
} else {
// if we've stopped intersecting, let's remove the animation
entry.target.classList.remove('view-animation');
}
});
}
})