Light scroll animation on pure JS. Code example open

Light scroll animation on pure JS. Code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
 // Scroll animations
  function onEntry(entry) {
    entry.forEach(change => {
      if (change.isIntersecting) {
        change.target.classList.add('element-show');
      }
    });
  }

  let options = {
    threshold: [0.5]
  };
  let observer = new IntersectionObserver(onEntry, options);
  let elements = document.querySelectorAll('.element-animation');

  for (let elm of elements) {
    observer.observe(elm);
  }

IF you want remove .element-show class after element leaving the viewport

document.addEventListener('DOMContentLoaded', () => {
    if (document.querySelector('.element-animation')) {
        function onEntry(entry) {
            entry.forEach(change => {
                if (change.isIntersecting) {
                    change.target.classList.add("element-show");
                } else {
                    change.target.classList.remove("element-show");
                }
            });
        }

        let options = {
            threshold: [0.5]
        };
        let observer = new IntersectionObserver(onEntry, options);
        let elements = document.querySelectorAll(".element-animation");

        for (let elm of elements) {
            observer.observe(elm);
        }
    }
});
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation