Home>Article>Web Front-end> How to set scroll bar position in javascript

How to set scroll bar position in javascript

coldplay.xixi
coldplay.xixi Original
2021-04-30 16:22:59 14088browse

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);

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

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; }

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!

Statement:
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 admin@php.cn