IntersectionObserver callback firing immediately on page load











up vote
1
down vote

favorite












I'm very new to the IntersectionObserver API, and I've been experimenting with this code:



let target = document.querySelector('.lazy-load');

let options = {
root: null,
rootMargin: '0px',
threshold: 0
}

let observer = new IntersectionObserver(callback, options);

observer.observe(target);

function callback() {
console.log('observer triggered.');
}


This seems to work as it should, and callback() is called whenever .lazy-load element enters the viewport, but callback() also fires once when the page is initially loaded, which triggers `console.log('observer triggered.');



Is there a reason for this callback to be triggered when the page loads? Or is there a mistake in how I'm implementing this?



Edit: Altering the code to the below still fires the callback at page load.



let target = document.querySelector('.lazy-load');

let options = {
root: null,
rootMargin: '0px',
threshold: 0
}

let callback = function(entries, observer) {
entries.forEach(entry => {

console.log('observer triggered.');

});
};

let observer = new IntersectionObserver(callback, options);

observer.observe(target);









share|improve this question




























    up vote
    1
    down vote

    favorite












    I'm very new to the IntersectionObserver API, and I've been experimenting with this code:



    let target = document.querySelector('.lazy-load');

    let options = {
    root: null,
    rootMargin: '0px',
    threshold: 0
    }

    let observer = new IntersectionObserver(callback, options);

    observer.observe(target);

    function callback() {
    console.log('observer triggered.');
    }


    This seems to work as it should, and callback() is called whenever .lazy-load element enters the viewport, but callback() also fires once when the page is initially loaded, which triggers `console.log('observer triggered.');



    Is there a reason for this callback to be triggered when the page loads? Or is there a mistake in how I'm implementing this?



    Edit: Altering the code to the below still fires the callback at page load.



    let target = document.querySelector('.lazy-load');

    let options = {
    root: null,
    rootMargin: '0px',
    threshold: 0
    }

    let callback = function(entries, observer) {
    entries.forEach(entry => {

    console.log('observer triggered.');

    });
    };

    let observer = new IntersectionObserver(callback, options);

    observer.observe(target);









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm very new to the IntersectionObserver API, and I've been experimenting with this code:



      let target = document.querySelector('.lazy-load');

      let options = {
      root: null,
      rootMargin: '0px',
      threshold: 0
      }

      let observer = new IntersectionObserver(callback, options);

      observer.observe(target);

      function callback() {
      console.log('observer triggered.');
      }


      This seems to work as it should, and callback() is called whenever .lazy-load element enters the viewport, but callback() also fires once when the page is initially loaded, which triggers `console.log('observer triggered.');



      Is there a reason for this callback to be triggered when the page loads? Or is there a mistake in how I'm implementing this?



      Edit: Altering the code to the below still fires the callback at page load.



      let target = document.querySelector('.lazy-load');

      let options = {
      root: null,
      rootMargin: '0px',
      threshold: 0
      }

      let callback = function(entries, observer) {
      entries.forEach(entry => {

      console.log('observer triggered.');

      });
      };

      let observer = new IntersectionObserver(callback, options);

      observer.observe(target);









      share|improve this question















      I'm very new to the IntersectionObserver API, and I've been experimenting with this code:



      let target = document.querySelector('.lazy-load');

      let options = {
      root: null,
      rootMargin: '0px',
      threshold: 0
      }

      let observer = new IntersectionObserver(callback, options);

      observer.observe(target);

      function callback() {
      console.log('observer triggered.');
      }


      This seems to work as it should, and callback() is called whenever .lazy-load element enters the viewport, but callback() also fires once when the page is initially loaded, which triggers `console.log('observer triggered.');



      Is there a reason for this callback to be triggered when the page loads? Or is there a mistake in how I'm implementing this?



      Edit: Altering the code to the below still fires the callback at page load.



      let target = document.querySelector('.lazy-load');

      let options = {
      root: null,
      rootMargin: '0px',
      threshold: 0
      }

      let callback = function(entries, observer) {
      entries.forEach(entry => {

      console.log('observer triggered.');

      });
      };

      let observer = new IntersectionObserver(callback, options);

      observer.observe(target);






      javascript lazy-loading intersection-observer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 19:25

























      asked Nov 8 at 18:36









      rpivovar

      6741728




      6741728
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          That is the default behaviour. When you instantiate an instance of the IntersectionObserver, the callback will be fired.



          It is recommended to guard against this case.



          entries.forEach(entry => {
          if (entry.intersectionRatio > 0) {
          entry.target.classList.add('in-viewport');
          } else {
          entry.target.classList.remove('in-viewport');
          }
          });


          Also I found this article as well as the docs to be very helpful, specifically about the intersectionRatio or isIntersecting properties on the IntersectionObserverEntry.



          · https://www.smashingmagazine.com/2018/01/deferring-lazy-loading-intersection-observer-api/



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



          · https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry






          share|improve this answer





















          • Awesome answer. Thanks.
            – rpivovar
            Nov 20 at 2:18











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214116%2fintersectionobserver-callback-firing-immediately-on-page-load%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          That is the default behaviour. When you instantiate an instance of the IntersectionObserver, the callback will be fired.



          It is recommended to guard against this case.



          entries.forEach(entry => {
          if (entry.intersectionRatio > 0) {
          entry.target.classList.add('in-viewport');
          } else {
          entry.target.classList.remove('in-viewport');
          }
          });


          Also I found this article as well as the docs to be very helpful, specifically about the intersectionRatio or isIntersecting properties on the IntersectionObserverEntry.



          · https://www.smashingmagazine.com/2018/01/deferring-lazy-loading-intersection-observer-api/



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



          · https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry






          share|improve this answer





















          • Awesome answer. Thanks.
            – rpivovar
            Nov 20 at 2:18















          up vote
          2
          down vote



          accepted










          That is the default behaviour. When you instantiate an instance of the IntersectionObserver, the callback will be fired.



          It is recommended to guard against this case.



          entries.forEach(entry => {
          if (entry.intersectionRatio > 0) {
          entry.target.classList.add('in-viewport');
          } else {
          entry.target.classList.remove('in-viewport');
          }
          });


          Also I found this article as well as the docs to be very helpful, specifically about the intersectionRatio or isIntersecting properties on the IntersectionObserverEntry.



          · https://www.smashingmagazine.com/2018/01/deferring-lazy-loading-intersection-observer-api/



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



          · https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry






          share|improve this answer





















          • Awesome answer. Thanks.
            – rpivovar
            Nov 20 at 2:18













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          That is the default behaviour. When you instantiate an instance of the IntersectionObserver, the callback will be fired.



          It is recommended to guard against this case.



          entries.forEach(entry => {
          if (entry.intersectionRatio > 0) {
          entry.target.classList.add('in-viewport');
          } else {
          entry.target.classList.remove('in-viewport');
          }
          });


          Also I found this article as well as the docs to be very helpful, specifically about the intersectionRatio or isIntersecting properties on the IntersectionObserverEntry.



          · https://www.smashingmagazine.com/2018/01/deferring-lazy-loading-intersection-observer-api/



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



          · https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry






          share|improve this answer












          That is the default behaviour. When you instantiate an instance of the IntersectionObserver, the callback will be fired.



          It is recommended to guard against this case.



          entries.forEach(entry => {
          if (entry.intersectionRatio > 0) {
          entry.target.classList.add('in-viewport');
          } else {
          entry.target.classList.remove('in-viewport');
          }
          });


          Also I found this article as well as the docs to be very helpful, specifically about the intersectionRatio or isIntersecting properties on the IntersectionObserverEntry.



          · https://www.smashingmagazine.com/2018/01/deferring-lazy-loading-intersection-observer-api/



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



          · https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 2:14









          snewcomer

          130110




          130110












          • Awesome answer. Thanks.
            – rpivovar
            Nov 20 at 2:18


















          • Awesome answer. Thanks.
            – rpivovar
            Nov 20 at 2:18
















          Awesome answer. Thanks.
          – rpivovar
          Nov 20 at 2:18




          Awesome answer. Thanks.
          – rpivovar
          Nov 20 at 2:18


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214116%2fintersectionobserver-callback-firing-immediately-on-page-load%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini