JavaScript Implementation Blog Garden page lower right corner return top button _javascript Tips

Source: Internet
Author: User
Tags setinterval

Blog Park in many bloggers blog in the lower right corner of the page has an icon, regardless of how the screen stretching, always stay in the lower right corner. Click on the top of the page. I want to write a demo to achieve this effect.

The lower right corner of the icon is fixed.

1.SS provides a 4-way layout. Where fixed represents an absolute positioning element. So we chose to use fixed to implement the icon fixed.

Absolute

Generates an absolutely positioned element that is positioned relative to the first parent element other than static positioning.

The position of the element is defined by the "left", "Top", "right" and "bottom" properties.

Fixed

Generates an absolutely positioned element that is positioned relative to the browser window.

The position of the element is defined by the "left", "Top", "right" and "bottom" properties.

Relative

Generates a relatively positioned element that is positioned relative to its normal position.

Therefore, "left:20" adds 20 pixels to the left position of the element.

Static The default value. There is no positioning, and the elements appear in the normal stream (ignoring top, bottom, leave, right, or z-index declarations).
Inherit Specify that the value of the position attribute should be inherited from the parent element.

2. Set the code as follows. button buttons will always be placed in the lower-right corner of the screen. Either drag the top or bottom bar or stretch the browser window size.

Copy Code code as follows:

#myTopBtn {
bottom:5px;
right:5px;
position:fixed;
}

Two. Realize click to return to the top corner of the page.

1. It's a little easier to get back to the top of the screen. Learn how to manipulate the drag bar up and down through JavaScript. JavaScript provides Scrollby and scroll methods.

Copy Code code as follows:

Window.scrollby (0,-30)//screen up 30 pixel points
Window.Scroll (0,0)//screen back to the top corner

2. The above mentioned how to move the drag bar, then how to achieve at a certain speed to move to the top of the page. Then you need to use the SetInterval and Clearinterval methods. Achieve a 10-millisecond screen move up to 30 pixel points.

Copy Code code as follows:

<body>
<div id= "Mydiv" >
</div>
<button id= "mytopbtn" onclick= "Topfunc ()" >to top</button>
</body>
 

Copy Code code as follows:

var MyVar;
function Topfunc () {
Myvar=setinterval (eachscrollby,10);
}

function Eachscrollby (eachheight) {
if (document.documentelement.scrolltop<=0) {
Clearinterval (MyVar);
}else{
Window.scrollby (0,-30);
}
}

Three. Extended

The top button is implemented. So how do we implement the Click Button screen bottom. In fact, the principle is similar, here will not write demo. To provide you with some attributes for reference.

Copy Code code as follows:

Page visible area wide: document.body.clientWidth
Page visible Area High: document.body.clientHeight
Web page Visible area wide: document.body.offsetWidth (including edge width)
Page visible Area High: document.body.offsetHeight (including edge width)
Page body Full text width: document.body.scrollWidth
The full text of the Web page High: document.body.scrollHeight
Web pages are rolled up high: Document.body.scrollTop
Pages were rolled away to the left: Document.body.scrollLeft
Web page body part: Window.screentop
Page body part left: Window.screenleft
High screen resolution: Window.screen.height
Width of screen resolution: Window.screen.width
Screen available Workspace height: window.screen.availHeight
Screen available workspace width: window.screen.availWidth

The above is the entire content of this article, I hope love to play blog children's shoes like.

Related Article

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.