Make a div appear when scrolling past a certain point of a page

后端 未结 3 550
逝去的感伤
逝去的感伤 2020-12-15 09:47

My goal is to make a fixed div appear at the top of a page once someone scrolls a certain amount of pixels down the page. Basically once the header section is out of view,

相关标签:
3条回答
  • 2020-12-15 10:15
    window.addEventListener("scroll",function() { 
       $('.fixedDiv')[(window.scrollY > 500)?"slideDown":"slideUp"]();
    },false);
    

    DEMO: http://jsfiddle.net/DerekL/8eG2A/

    0 讨论(0)
  • 2020-12-15 10:17
    window.addEventListener("scroll",function() { 
       if(window.scrollY > 500) {
          $('.fixedDiv').slideDown();
       }
       else {
          $('.fixedDiv').slideUp();
       }
    },false);
    
    0 讨论(0)
  • 2020-12-15 10:36

    Brandon Tilley answered my question in a comment...

    You would change the first line, with the startY, to be the specific Y position you need, rather than calculating based on the header's position and height. Here's an updated fiddle: jsfiddle.net/BinaryMuse/Ehney/1

    0 讨论(0)
提交回复
热议问题