Use JavaScript to obtain information such as the scroll bar position

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.