English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The scrollTop() method gets or sets the vertical scrollbar position of the selected element.
When using the scrollTop() methodGetposition, it returnsthe first selected elementthe vertical position of the scrollbar.
When using the scrollTop() methodSetWhen the position is set, it will beAll selected elementsSet the vertical position of the scrollbar.
Get the position of the vertical scrollbar:
$.scrollTop()
Set the position of the vertical scrollbar:
$.scrollTop(value)
Get the scrollTop of DIV:
$("div").scroll(function(){ $(this).scrollTop(); });Test See‹/›
Set the scrollTop of DIV:
$("button").click(function(){ $("div").scrollTop(150); });Test See‹/›
Set the document's scrollTop:
$("button").click(function(){ $(document).scrollTop(400); });Test See‹/›
Add smooth scrolling when the user scrolls the page:
let size = $(".main").height(); // Get the height of ".main" $(window).keydown(function(event) { if(event.which === 40) { // If the down arrow key is pressed $("html, body").animate({scrollTop: "+=" + size}, 300); } else if(event.which === 38) { // If the up arrow key is pressed $("html, body").animate({scrollTop: "-=" + size}, 300); } });Test See‹/›
Parameter | Description |
---|---|
value | An integer indicating the new position to set the scrollbar to |