Ben
Ben

Reputation: 2564

jquery detect scroll bar position

I have a button, when scroll bar is bigger than 600, it will show, if less it will hide.

I'm not sure where is the problem, it is not working.

var scrollTop = $(window).scrollTop();

    if(scrollTop<600){$('#top').hide();}
    else{$('#top').show();}

Upvotes: 0

Views: 69

Answers (1)

John Dorean
John Dorean

Reputation: 3874

You need to bind the scroll() event to the window, rather than checking the scroll position once.

Try something like this:

$(window).scroll(function() {
    var scrollTop = $(window).scrollTop();

    if(scrollTop < 600) {
        $('#top').hide();
    } else {
        $('#top').show();
    }
});

Upvotes: 5

Related Questions