How to detect vertical scroll position using jquery?

omega picture omega · Aug 2, 2013 · Viewed 21.8k times · Source

Using jquery, how can you set an event that triggers when there is a vertical scroll visible, and when you change from top half of page to bottom half of page, and vice versa.

So for example, if the vertical scroll bar is there, and then I am looking somewhere on the page which is in the top half of the page, and then move down so I am in the bottom half of the page, the function happens. Then if I move back up so I am in the top half again, the function happens.

Thanks.

Answer

raam86 picture raam86 · Aug 2, 2013
var midHeight = jQuery(window).height() / 2 //Splits screen in half
$(window).scroll(function () {
    if ($(window).scrollTop() > midHeight) {
        //Do something on bottom
    } else {
        //Do something on top
    }
})