JS get the scroll bar from the top of the browser, the height of the bottom __js

Source: Internet
Author: User

Web development often encounters the need to get the browser's scroll bar and the top and bottom of the distance, and then do the appropriate processing action. The following author on how to get through JS browser scroll bar from the top of the browser and the bottom of the height to do a share, this is compatible with IE and Firefox.

The first two definitions we need to know are:

The height of the scroll bar from the top of the browser = window scroll bar height;

The height of the scroll bar from the bottom of the browser = The actual height of the document (page) content-the height of the scroll bar from the top of the browser-the height of the window's viewable range;

Well, with this definition, let's take a look at how to get the various heights, and here's an example.

Gets the height of the window's viewable range

Function getclientheight () {         var clientHeight=0;          if (document.body.clientheight&& Document.documentElement.clientHeight) {              var clientheight= (document.body.clientheight<document.documentelement.clientheight)? document.body.clientheight:document.documentelement.clientheight;                  }else{              var clientheight= (document.body.clientheight> Document.documentElement.clientHeight)?document.body.clientheight:document.documentelement.clientheight;              }          return clientHeight;    } 

Get window scroll bar height

function Getscrolltop () {var scrolltop=0; if (document.documentelement&&document.documentelement.scrolltop) {Scrolltop=document.documentelement.s         Crolltop;         }else if (document.body) {scrolltop=document.body.scrolltop;     return scrolltop; }

Get the actual height of the document content

function Getscrollheight () {return Math.max (document.body.scrollheight,document.documentelement.scrollheight); }

The following is a specific sample code, note here in order to demonstrate the effect of using a fixed suspension box effect, under IE fixed suspension effect and the above code some conflict does not work, here do not delve into, readers can see under the Firefox effect, the code itself is no problem.

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.