Javascript 如果滚动位置 > 100

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17844276/
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-10-27 09:49:59  来源:igfitidea点击:

Javascript if scroll position > 100

javascriptscrollalertscrolltop

提问by Alpan Karaca

How can I test it if it's greater than 100. For example how can I get an alert.

如果大于 100,我如何测试它。例如,我如何获得警报。

Here is my code that doesn't work, I don't know why.

这是我的代码不起作用,我不知道为什么。

var ScrollTop = $("body").scrollTop();

if (ScrollTop > 100){
    alert("Scroll is greater than 100");
    //document.getElementById('back-to-top').fadeOut;
}

采纳答案by verbanicm

Use a scroll listener http://jsfiddle.net/6A6qy/

使用滚动侦听器http://jsfiddle.net/6A6qy/

$(window).scroll(function(){...});

回答by HIRA THAKUR

PERFECT FIDDLE EXAMPLE

完美的小提琴示例

var el = $('.test');

el.on('scroll', function(){

  alert(el.scrollTop());


});

回答by mikach

Try to take $(document).scrollTop() instead of $('body)

尝试使用 $(document).scrollTop() 而不是 $('body)