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 do something when window on needed heigth

I have some HTML code

<div class="mission" id="mission_block">
            <div class="mission__img">
                <img src="img/phone90.png" alt="">
            </div>
            <div class="mission__text">
                <h1>Our mission and vision</h1>
                <p>While technology is our sweet spot, we’ve developed our vision alongside
                    our customers to ensure your everyday problems are solved through simplistic tools. And as your
                    business grows, we’ll grow right alongside with you. </p>
            </div>
        </div>

I need animate this blocks when window will be on the middle of block mission. How can I create trigger for this?

I already have such JS code:

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

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

      const sizes = missionBlock.getBoundingClientRect();

      const {height, top} = sizes
      const scrollTriggerHeigth = top + height / 2

   }
})(window, document, undefined)

>Solution :

You could do this easily with Intersection Observer API.

E.g.

const missionBlock = document.getElementById("mission_block");

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

    if (entry.isIntersecting) {
      // do here whatever you need to do
      console.log("instersecting", entry);
      doWhatever();
    }
  },
  { threshold: 0.5 }
);

observer.observe(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