Many bloggers in the blog park have an icon in the lower right corner of Page in the blog. No matter how the screen stretches, it always stays in the lower right corner. Click the page to top. Let’s think about writing a demo to achieve this effect later.
1. The lower right corner of the icon is fixed.
1. SS provides 4 layout methods. Fixed represents absolute positioning elements. So we chose to use fixed to achieve icon fixation.
| absolute | Generates absolutely positioned elements, positioning relative to the first parent element other than static positioning. The position of an element is specified by the "left", "top", "right" and "bottom" attributes. |
| fixed | Generate absolutely positioned elements, positioning relative to the browser window. The position of an element is specified by the "left", "top", "right" and "bottom" attributes. |
| Relative | Generate relatively positioned elements, positioning relative to their normal position. Therefore, "left:20" adds 20 pixels to the LEFT position of the element. |
| static | default value. There is no positioning, the element appears in the normal stream (ignoring the top, bottom, left, right or z-index declaration). |
| 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 is dragging the upper and lower precision bar or stretching the browser window size.
The code copy is as follows:
#myTopBtn{
bottom: 5px;
right: 5px;
position:fixed;
}
2. After clicking, return to the top corner of the page.
1. To return to the top corner of the screen, you need to understand how to use JavaScript to operate the up and down movement of the drag bar. JavaScript provides scrollby and scroll methods.
The code copy is as follows:
window.scrollBy(0,-30) //Move 30 pixels up the screen
window.scroll(0,0) // The screen returns to the top corner
2. The above mentioned how to move the drag bar, so how to move to the top of the page at a certain speed. Then you need to use the setInterval and clearInterval methods. This will enable 30 pixels to be moved up the screen in less than 10 milliseconds.
The code copy is as follows:
<body>
<div id="myDiv" >
</div>
<button id="myTopBtn" onclick="TopFunc()">To Top</button>
</body>
The code copy is 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);
}
}
III. Expand
The top button is implemented. So how do we implement the button click on the bottom screen? In fact, the principle is similar, so we won’t write demo here. Provide some attributes for reference.
The code copy is as follows:
The visible area width of the web page: document.body.clientWidth
The visible area height of the web page: document.body.clientHeight
The visible area width of the web page: document.body.offsetWidth (including the width of the edge line)
The visible area height of the web page: document.body.offsetHeight (including the width of the edge)
Full text width of the web page: document.body.scrollWidth
Full text of the web page: document.body.scrollHeight
The web page is rolled out at a high level: document.body.scrollTop
The left of the web page being rolled out: document.body.scrollLeft
On the main part of the web page: window.screenTop
Left of the main text of the web page: 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 that the children's shoes who love to play blogs like it.