Sometimes, in order to accurately locate an element, we need to obtain the position of the scroll bar. This requirement is often used in applications such as tooltip and drag-and-drop. In fact, this technology is very simple. The key is to consider browser compatibility, this article describes how to solve this problem.
In fact, this sectionCodeThis article has already been introduced in the previous article "using JavaScript to achieve smooth jump between anchor". However, due to this requirement and frequent use, this article is specially published on this site for reference.
Javascript:
1 < Script Type = " Text/JavaScript " >
2 // Notes: use JavaScript to obtain information such as the scroll bar position.
3 // Source: thickbox 2.1
4 // Sort by: codebit.cn (http://www.CodeBit.cn)
5 Function Getscroll (){
6 VaR T, L, W, h;
7 If (Document.doc umentelement && Document.doc umentelement. scrolltop ){
8 T = Document.doc umentelement. scrolltop;
9 L = Document.doc umentelement. scrollleft;
10 W = Document.doc umentelement. scrollwidth;
11 H = Document.doc umentelement. scrollheight;
12 } Else If (Document. Body ){
13 T = Document. Body. scrolltop;
14 L = Document. Body. scrollleft;
15 W = Document. Body. scrollwidth;
16 H = Document. Body. scrollheight;
17 }
18 Return {T: T, L: l, W: W, H: H };}
19 < / SCRIPT>