Intersection Observer when element leaves the viewport










3















Is there a way to detect if an element leaves the viewport using Intersection Observers? For example, I have an element on the screen that I want to fire a callback when the top of the element hits the top of the viewport. From MDN:




The Intersection Observer API lets code register a callback function that is executed whenever an element they wish to monitor enters or exits another element (or the viewport), or when the amount by which the two intersect changes by a requested amount. -- (https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)




So if I do something like below, I would have thought that the callback would have fired when the top of the element hit the top of the viewport as well?



var options = 
root: document.querySelector('#element'),
rootMargin: '0px',
threshold: 0


var observer = new IntersectionObserver(callback, options);


But it only seems to be firing when the top of the element scrolls in and hits the bottom of the viewport, but not both. Ideas?










share|improve this question


























    3















    Is there a way to detect if an element leaves the viewport using Intersection Observers? For example, I have an element on the screen that I want to fire a callback when the top of the element hits the top of the viewport. From MDN:




    The Intersection Observer API lets code register a callback function that is executed whenever an element they wish to monitor enters or exits another element (or the viewport), or when the amount by which the two intersect changes by a requested amount. -- (https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)




    So if I do something like below, I would have thought that the callback would have fired when the top of the element hit the top of the viewport as well?



    var options = 
    root: document.querySelector('#element'),
    rootMargin: '0px',
    threshold: 0


    var observer = new IntersectionObserver(callback, options);


    But it only seems to be firing when the top of the element scrolls in and hits the bottom of the viewport, but not both. Ideas?










    share|improve this question
























      3












      3








      3








      Is there a way to detect if an element leaves the viewport using Intersection Observers? For example, I have an element on the screen that I want to fire a callback when the top of the element hits the top of the viewport. From MDN:




      The Intersection Observer API lets code register a callback function that is executed whenever an element they wish to monitor enters or exits another element (or the viewport), or when the amount by which the two intersect changes by a requested amount. -- (https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)




      So if I do something like below, I would have thought that the callback would have fired when the top of the element hit the top of the viewport as well?



      var options = 
      root: document.querySelector('#element'),
      rootMargin: '0px',
      threshold: 0


      var observer = new IntersectionObserver(callback, options);


      But it only seems to be firing when the top of the element scrolls in and hits the bottom of the viewport, but not both. Ideas?










      share|improve this question














      Is there a way to detect if an element leaves the viewport using Intersection Observers? For example, I have an element on the screen that I want to fire a callback when the top of the element hits the top of the viewport. From MDN:




      The Intersection Observer API lets code register a callback function that is executed whenever an element they wish to monitor enters or exits another element (or the viewport), or when the amount by which the two intersect changes by a requested amount. -- (https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)




      So if I do something like below, I would have thought that the callback would have fired when the top of the element hit the top of the viewport as well?



      var options = 
      root: document.querySelector('#element'),
      rootMargin: '0px',
      threshold: 0


      var observer = new IntersectionObserver(callback, options);


      But it only seems to be firing when the top of the element scrolls in and hits the bottom of the viewport, but not both. Ideas?







      javascript intersection-observer






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 18:12









      Barry HorbalBarry Horbal

      161




      161






















          2 Answers
          2






          active

          oldest

          votes


















          0














          If I correctly understand what you're trying to do, you could set rootMargin to '0px 0px -100%'. Also, threshold: 0 is the default, so you don't need to include it in your options object.






          share|improve this answer






























            0














            If you go through the API doc here, the threshold option can be passed as an array to define on what levels of intersection should the callback be fired. So, passing something like [0, 0.8] fires the callback each time the element is visible by at least 80% and also when the element is not visible (exits the viewport).



            There might be other complications while doing this. For e.g. if there are multiple elements, whenever one element is visible, other element might exit the viewport hence firing unnecessary callbacks. I handled it using some additional if conditions in my case. I also had to do some hit and trials to determine the best suitable values of threshold and intersectionRatio.






            share|improve this answer
























              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',
              autoActivateHeartbeat: false,
              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%2f53306419%2fintersection-observer-when-element-leaves-the-viewport%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              If I correctly understand what you're trying to do, you could set rootMargin to '0px 0px -100%'. Also, threshold: 0 is the default, so you don't need to include it in your options object.






              share|improve this answer



























                0














                If I correctly understand what you're trying to do, you could set rootMargin to '0px 0px -100%'. Also, threshold: 0 is the default, so you don't need to include it in your options object.






                share|improve this answer

























                  0












                  0








                  0







                  If I correctly understand what you're trying to do, you could set rootMargin to '0px 0px -100%'. Also, threshold: 0 is the default, so you don't need to include it in your options object.






                  share|improve this answer













                  If I correctly understand what you're trying to do, you could set rootMargin to '0px 0px -100%'. Also, threshold: 0 is the default, so you don't need to include it in your options object.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 25 at 20:37









                  Jonathan MelinJonathan Melin

                  1




                  1























                      0














                      If you go through the API doc here, the threshold option can be passed as an array to define on what levels of intersection should the callback be fired. So, passing something like [0, 0.8] fires the callback each time the element is visible by at least 80% and also when the element is not visible (exits the viewport).



                      There might be other complications while doing this. For e.g. if there are multiple elements, whenever one element is visible, other element might exit the viewport hence firing unnecessary callbacks. I handled it using some additional if conditions in my case. I also had to do some hit and trials to determine the best suitable values of threshold and intersectionRatio.






                      share|improve this answer





























                        0














                        If you go through the API doc here, the threshold option can be passed as an array to define on what levels of intersection should the callback be fired. So, passing something like [0, 0.8] fires the callback each time the element is visible by at least 80% and also when the element is not visible (exits the viewport).



                        There might be other complications while doing this. For e.g. if there are multiple elements, whenever one element is visible, other element might exit the viewport hence firing unnecessary callbacks. I handled it using some additional if conditions in my case. I also had to do some hit and trials to determine the best suitable values of threshold and intersectionRatio.






                        share|improve this answer



























                          0












                          0








                          0







                          If you go through the API doc here, the threshold option can be passed as an array to define on what levels of intersection should the callback be fired. So, passing something like [0, 0.8] fires the callback each time the element is visible by at least 80% and also when the element is not visible (exits the viewport).



                          There might be other complications while doing this. For e.g. if there are multiple elements, whenever one element is visible, other element might exit the viewport hence firing unnecessary callbacks. I handled it using some additional if conditions in my case. I also had to do some hit and trials to determine the best suitable values of threshold and intersectionRatio.






                          share|improve this answer















                          If you go through the API doc here, the threshold option can be passed as an array to define on what levels of intersection should the callback be fired. So, passing something like [0, 0.8] fires the callback each time the element is visible by at least 80% and also when the element is not visible (exits the viewport).



                          There might be other complications while doing this. For e.g. if there are multiple elements, whenever one element is visible, other element might exit the viewport hence firing unnecessary callbacks. I handled it using some additional if conditions in my case. I also had to do some hit and trials to determine the best suitable values of threshold and intersectionRatio.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 15 at 10:08

























                          answered Mar 15 at 10:00









                          PragunPragun

                          728215




                          728215



























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306419%2fintersection-observer-when-element-leaves-the-viewport%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







                              Popular posts from this blog

                              Kleinkühnau

                              Makov (Slowakei)

                              Deutsches Schauspielhaus