jQuery 当发生一定量的滚动时隐藏元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6820769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:25:17  来源:igfitidea点击:

Hide an element when a certain amount of scrolling has occured

jqueryanimationscroll

提问by Fuego DeBassi

I'd just like to hide an element on my page, after N number of pixels have been scrolled.

在滚动了 N 个像素后,我只想在我的页面上隐藏一个元素。

$(window).scroll(function(){
  if($(document).scrollTop() > 200){
    $('.fixedelement').css({'display': 'none'});
  }
});

I thought this might work, and after 200px of scrolling the .fixedelement would vanish. Alas, it doesn't work. Any thoughts?

我认为这可能会奏效,滚动 200 像素后 .fixedelement 会消失。唉,它不起作用。有什么想法吗?

回答by Naftali aka Neal

It seems to work fine here: http://jsfiddle.net/maniator/yDVXY/

它似乎在这里工作正常:http: //jsfiddle.net/maniator/yDVXY/

$(window).scroll(function() {
    if ($(this).scrollTop() > 200) { //use `this`, not `document`
        $('.fixedelement').css({
            'display': 'none'
        });
    }
});

回答by ShankarSangoli

Try this.

尝试这个。

$(window).scroll(function(){
  if($(document).scrollTop() > 200){//Here 200 may be not be exactly 200px
    $('.fixedelement').hide();
  }
});