JS Gets the height of the window scrollbar, the height of the window's visible range, the actual content height of the document, the height of the scroll bar from the bottom of the browser

Source: Internet
Author: User

1. Get the height of the window's visible range

1 //gets the height of the window's visible range2 functiongetclientheight () {3     varClientheight=0; 4     if(document.body.clientheight&&document.documentElement.clientHeight) {  5         varclientheight= (document.body.clientheight<document.documentelement.clientheight)?Document.body.clientHeight:document.documentElement.clientHeight;6}Else{  7         varclientheight= (document.body.clientheight>document.documentelement.clientheight)?Document.body.clientHeight:document.documentElement.clientHeight;8     }  9     returnclientheight; Ten}

2. Get the height of the window scroll bar

1 functionGetscrolltop () {2     varScrolltop=0; 3     if(document.documentelement&&Document.documentElement.scrollTop) {  4scrolltop=Document.documentElement.scrollTop; 5}Else if(document.body) {6scrolltop=Document.body.scrollTop; 7     }  8     returnscrolltop; 9}

3. Get the actual height of the document content

1 function getscrollheight () {  2     return  Math.max (Document.body.scrollHeight, document.documentElement.scrollHeight);   3 }

4. How to use

1Window.onscroll=function(){2     //Height of window visual range3     varheight=getclientheight (),4         //window scroll bar height5theight=getscrolltop (),6         //Height of window visual range7rheight=getscrollheight (),8         //height of scroll bar distance from bottom9bheight=rheight-theight-height;Ten          Onedocument.getElementById (' show '). Innerhtml= ' At this time the browser visible area height is: ' +height+ ' <br/> At this time the actual height of the document content is: ' +rheight+ ' <br/ > The height at the top of the scrollbar is: ' +theight+ ' <br/> The height at the bottom of the scroll bar is: ' +Bheight; A}

JS Gets the height of the window scrollbar, the height of the window's visible range, the actual content height of the document, the height of the scroll bar from the bottom of the browser

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.