Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove IntersectionObserver correct

I have animation on enter window on middle of block made with IntersectionObserver. And I dont know how correct remove observing after first animation.

My code:

((window, document) => {
   window.onload = () => {
      const missionBlock = document.getElementById('mission_block');
      const missionText = document.getElementById('mission_text');
      const missionImg = document.getElementById('mission_img');

      let observer = new IntersectionObserver(
         (entries) => {
            // Take the first entry
            const entry = entries[0];

            if (entry.isIntersecting) {
               missionImg.style.transform = "translateX(-70%)"
              missionImg.style.transition = "transform 1s ease-in-out"

               missionText.style.opacity = "0.5"
               missionText.style.transform = "translateX(235%)"
               missionText.style.transition = "opacity 1s ease-in-out"
               missionText.style.transition = "transform 1s ease-in-out"

            }
         },
         { threshold: 0.5 }
      );

      observer.observe(missionBlock);

   }
})(window, document, undefined)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Is it hepls?

The IntersectionObserver method unobserve() instructs the IntersectionObserver to stop observing the specified target element.

https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/unobserve

//...
if (entry.isIntersecting) {
    observer.unobserve(missionBlock);
//...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading