jquery:检测滚动位置

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

jquery : detecting scroll position

javascriptjqueryscrollfooter

提问by Mag

I want to get an alert when, while scrolling, my footer comes to view.

我想在滚动时看到我的页脚时收到警报。

$(window).on("mousewheel", function(){
    if ($(window).scrollTop() + $(window).height() > $('#footer').position().top){    
        alert("footer visible");
    }  
    else{
        alert("footer invisible");  
    }
});

http://jsfiddle.net/JRUnr/10/

http://jsfiddle.net/JRUnr/10/

All conditions with height seem right, but not during scrolling.

所有具有高度的条件似乎都正确,但在滚动期间则不然。

回答by SarathSprakash

Working DEMO

工作演示

Try this

尝试这个

$(window).scroll(function () {

    if ($(window).scrollTop() + $(window).height() > $('.footer').offset().top) {
        alert("footer visible");
    } else {
        alert("footer invisible");
    }
});

Hope this helps,Thank you

希望这有帮助,谢谢

回答by Alex Tselegidis

There is a jquery plugin for this task named jQuery Waypoints (http://imakewebthings.com/jquery-waypoints/)

此任务有一个名为 jQuery Waypoints 的 jquery 插件(http://imakewebthings.com/jquery-waypoints/

$('#footer').waypoint(function(direction) {
    alert('Top of thing hit top of viewport.');
});

回答by kasper Taeymans

here is a working fiddle... http://jsfiddle.net/kasperfish/JRUnr/14/

这是一个工作小提琴...... http://jsfiddle.net/kasperfish/JRUnr/14/

it is hacked together but it works

它被黑客攻击在一起,但它有效

        flag=true;


$(window).scroll(function() {
    st=$(window).scrollTop();
    $('#topscroll').html(st)


    if(st>1450){
        if(flag)
        alert('test');flag=false;
    }

});