Reputation: 559
How to achieve the following functionality? When the scroll-bar reaches the certain point, I need to scroll up for a certain amount of pixels?
Not that good with JS/jQuery, maybe to bind scroll event to document, but really don't know how to achieve this.
This is what I have, but it doesn't work:
$(document).bind('scroll',function(e){
if (
$(this).offset().top > 3000)
{
$.scrollTo('-800px', 800, { axis:'y' });
}
});
Upvotes: 1
Views: 514
Reputation: 1587
Try comparing document.body.scrollTop
with document.body.clientHeight
. If it reaches your desired point use the scrollBy()
method to go up some pixels.
EDIT after your update:
You’re mixing up scrollTo
(scroll to absolute position on your page) and scrollBy
(scroll up or down from the current scroll position). Also, these are no jQuery methods, so use them without the $.
EDIT 2:
You can’t use offset()
for the scroll position. It only gets the position of an element on your page.
Here’s a working example:
$(document).bind('scroll', function(){
if (document.body.scrollTop > 3000) {
scrollBy(0, -800);
}
});
Upvotes: 1