jquery : detecting scroll position

こ雲淡風輕ζ 提交于 2019-12-17 11:29:10

问题


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/

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


回答1:


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




回答2:


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

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



回答3:


here is a working fiddle... 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;
    }

});


来源:https://stackoverflow.com/questions/18930758/jquery-detecting-scroll-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!