Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect vertical scroll position using jquery?

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.

like image 497
omega Avatar asked Jun 05 '26 12:06

omega


2 Answers

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
    }
})
like image 134
raam86 Avatar answered Jun 08 '26 02:06

raam86


well jQuery does have these methods you can use on an element:

.scrollTop() - get top scroll position on an element in the viewport

.scrollLeft() - get left scroll position on an element in the viewport

Also there is the scroll event - that triggers when a viewport/element scrolls within another one (or the window):

.scroll()

like image 29
revlayle Avatar answered Jun 08 '26 02:06

revlayle