Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop page scroll after the page reaches certain point

How can scroll be prevented after scrollTop reaches certain value say 150.

$(window).scroll(function() {   
    if($(window).scrollTop() >=50)) {
       return false;    // basically don't scroll       
    }
});
like image 586
user1184100 Avatar asked Jan 16 '23 18:01

user1184100


1 Answers

the .scrollTop set the scrollHeight using scrollTo function. It doesn't scroll from x to y, It just goes to y.

So basically you cannot stop the scroll as your event will be called after it is set to y. You can probably set the desired scrollHeight inside the handler after comparing the height.

if($(window).scrollTop() >=50) 
{ 
    $(window).scrollTop(0); 
}

Note: using it on an element is bearable, but on window object will be annoying to the user. Above is just to show how it works.

Try scrolling in >> http://jsfiddle.net/KwgMj << and see how annoying it can be.

like image 75
Selvakumar Arumugam Avatar answered Jan 29 '23 05:01

Selvakumar Arumugam