Home >Web Front-end >JS Tutorial >Javascript implements the return to top button in the lower right corner of the blog page_javascript skills
Many bloggers in the blog garden have an icon in the lower right corner of the page in their blogs. No matter how the screen is stretched, it always stays in the lower right corner. Click to pin the page to the top. Think about writing a Demo later to achieve this effect.
1. The lower right corner of the icon is fixed.
1.SS provides 4 layout methods. Fixed means absolutely positioned elements. So we choose to use fixed to achieve icon fixation.
absolute |
|
||||||||||
fixed | Generate an absolutely positioned element, positioned relative to the browser window. The position of the element is specified through the "left", "top", "right" and "bottom" attributes. | ||||||||||
relative | Generates a relatively positioned element, positioned relative to its normal position. So "left:20" adds 20 pixels to the element's LEFT position. | ||||||||||
static | Default value. Without positioning, the element appears in normal flow (ignoring top, bottom, left, right or z-index declarations). | ||||||||||
inherit | specifies that the value of the position attribute should be inherited from the parent element. |
2. The code is as follows. The Button button will always be placed in the lower right corner of the screen. Whether it’s dragging the upper and lower precision bars or stretching the browser window size.
2. Return to the top corner of the page after clicking.
1. If you want to return to the top corner of the screen, you need to know how to move the drag bar up and down through JavaScript. JavaScript provides the scrollby and scroll methods.
2. We have mentioned how to move the drag bar above, so how to move it to the top of the page at a certain speed. Then you need to use the setInterval and clearInterval methods to move the screen up by 30 pixels in every 10 milliseconds.
function EachScrollBy(eachHeight){
If(document.documentElement.scrollTop<=0){
clearInterval(myVar);
}else{
window.scrollBy(0,-30);
}
}
3. Extension
Implemented the pin button. So how do we realize that clicking the button puts the screen at the bottom? In fact, the principle is similar, so I won’t write a demo here. Provide you with some attributes for reference.
The above is the entire content of this article. I hope children who love blogging will like it.