简体   繁体   中英

need an event triggered if the page has scrolled 200px down (jQuery)

I want to show and hide a piece of code if i scroll and the page is for example half way, i have tried to use window scroll but this doesnt works(no errors, clean code, different browsers, different jQuery versions), but this doesn't trigger anything, so i am looking for a better way to show and hide a div if i scrolldown.

used this to trigger an event(not working)

$(window).scroll(function(){
    alert('works')
});

Try using the window.onload function (that's how they use it in jQuery examples):

window.onload = (function(){
  $(window).scroll(function () { 
    if( $(window).scrollTop() > 200 ) {
      // Display something
    }
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM