Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll top +10 with jQuery

Tags:

jquery

I want that when the user clicks one button, he scrolls +10 pixels down. I can't use $('html, body').animate({scrollTop: '10px'}, 300); here, because it will just scroll +10px from the top and that's not what I need. Is it possible to do what I want? Thanks.

like image 495
good_evening Avatar asked Feb 03 '26 12:02

good_evening


2 Answers

$('html, body').animate({scrollTop: ($(window).scrollTop() + 10) + 'px'}, 300);
like image 119
Kai Qing Avatar answered Feb 06 '26 04:02

Kai Qing


The scrollTop() jQuery function not only scrolls the page, but also returns the current scroll position.

To get the current scroll position, use var y = $('body').scrollTop(); then use that to calculate the number you need (y+10).

like image 44
Tim Withers Avatar answered Feb 06 '26 06:02

Tim Withers