JS to achieve a simple uniform speed back to the top of the page.

Source: Internet
Author: User

<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title> back to top </title>
<style>
body{
height:1600px;
}
. top{
width:100%;
height:30px;
background:red;
}
#box {
width:100px;
height:30px;
Background:pink;
line-height:30px;
position:fixed; --note here to use fixed positioning.
top:90%;
right:40px;
Display:none; --Start by setting the button back to the top to hide the property, and when the visible area height is at a certain value from the top of the page (scrolltop), change the dispaly to make it appear.
Cursor:pointer;
}
</style>
<script>
Window.onload = function () {
var box = document.getElementById ("box");
var top = 0;
var timer;
Window.onscroll = function () {
top = Document.documentElement.scrollTop | | Document.body.scrollTop;
if (Top > 600) {
Box.style.display = "block";
}else{
Box.style.display = "None";
}
}
Box.onclick = function () {
Clearinterval (timer);
var sudu = 10;
Timer = setinterval (function () {
Console.log (top); -----> Note Test the top value to avoid the case where the timer is not cleared.
if (top <= 0) {
Clearinterval (timer);
}else{
Document.documentElement.scrollTop = Document.body.scrollTop = Top-sudu;
}
},10);
}
}
</script>
<body>
<div class= "Top" ></div>
<div id= "box" >
Point me back to the top of
</div>
</body>

JS to achieve a simple uniform speed back to the top 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.