Home > Web Front-end > JS Tutorial > body text

How to set scroll bar position in javascript

coldplay.xixi
Release: 2023-01-05 16:13:22
Original
13936 people have browsed it

How to set the scroll bar position in javascript: 1. Put the vertical scroll bar at the top, the code is [(window.scrollTo(0,0);]]; 2. Put the vertical scroll bar at the bottom, the code is [window .scrollTo(0,document.body.scrollHeight)】.

How to set scroll bar position in javascript

##The operating environment of this tutorial: windows7 system, javascript version 1.8.5, DELL G3 computer.

How to set the scroll bar position in javascript:

JS controls the position of the scroll bar:

window.scrollTo(x,y);
Copy after login

The vertical scroll bar is on top (window.scrollTo(0, 0);

The vertical scroll bar is set to the bottom window.scrollTo(0,document.body.scrollHeight)

JS controls the TextArea scroll bar to automatically scroll to the bottom

document.getElementByIdx_x('textarea').scrollTop = document.getElementByIdx_x('textarea').scrollHeight
Copy after login

Get the distance from the top of the scroll bar

function getScrollTop(){
var scrollTop=0;
if(document.documentElement&&document.documentElement.scrollTop){
scrollTop=document.documentElement.scrollTop;
}else if(document.body){
scrollTop=document.body.scrollTop;
}
return scrollTop;
}
Copy after login

Related free learning recommendations:

javascript video tutorial

The above is the detailed content of How to set scroll bar position in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!