JS Operation Dom displays a floating box based on the scroll position of the page

Source: Internet
Author: User

When you do the page, you may use a fixed-position layer, read the DOCUMENT.BODY.SCROLLTOP to set the display floating box, like this:-

<!DOCTYPE HTML><HTML> <Head>    <MetaCharSet= "UTF-8">    <title>Show floating box based on page scroll position</title>    <style>Body{Height:2000px;}#toTop{position:fixed;Bottom:100px; Right:0;Display:None;    }  </style> </Head> <Body>    <DivID= "Totop">        <ahref="#">Back to Top</a>    </Div>  <Script>Window.onscroll=function(){    if(Document.body.scrollTop>= -) ToTop.style.display="Block"; ElseToTop.style.display=""; }    </Script> </Body></HTML>

But how did not achieve the expected effect, the output document.body.scrollTop value of a look, has been 0. It is a DTD problem, if the page directly with the beginning of the words will be no problem.

But to conform to web standards, of course, DTDs are not few. You can use DOCUMENT.DOCUMENTELEMENT.SCROLLTOP instead of DOCUMENT.BODY.SCROLLTOP when you have a DTD. -

1   <Script>2 Window.onscroll=function(){3     if(Document.documentElement.scrollTop>= -)4 ToTop.style.display="Block";5     Else6 ToTop.style.display="";7   }8     </Script>

Note: When the page has a DTD (or specifies DOCTYPE), use Document.documentelement.
When the page does not have a DTD (or no DOCTYPE specified), use Document.body.
This is true both in IE and in Firefox.
For compatibility, you can use the following code:

1234 | | 0;

Added: JS operation Dom and get browser height and width

1. Get the width of the visible area of the webpage: document.body.clientWidth;

2. Get the height of the visible area of the webpage: document.body.clientHeight;

3. Get the width of the visible area of the webpage: document.body.offsetWidth; (including the width of the edge)

4. Get the page visible area High: Document.body.offsetHeight;(including the width of the edge)

5. Get the full text width of the Web page: document.body.scrollWidth;

6. Get the full text of the Web page High: document.body.scrollHeight;

7. Get the page to be rolled High: document.body.scrollTop;

8. Get the page to be rolled left: document.body.scrollLeft;

9. Get the body part of the webpage: window.screentop;

11. Get the page body part left: window.screenleft;

12. Get the high screen resolution: window.screen.height;

13. Get the width of the screen resolution: Window.screen.width;

14. Screen available work area height: window.screen.availHeight

15. Screen available work area width: window.screen.availWidth

JS Operation Dom displays a floating box based on the scroll position of the page

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.