jquery : detecting scroll position

2019-01-11 16:42发布

问题:

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;
    }

});