Implementation of the return top button in javascript
- 2020-06-07 03:57:21
- OfStack
Cool back to top feature
js core code
window.onscroll = function(){
var t = document.documentElement.scrollTop || document.body.scrollTop;
var top_div = document.getElementById( "go" );
if( t >= 300 ) {
top_div.style.display = "block";
} else {
top_div.style.display = "none";
}
}
var ptt=20;
function pageScroll() {
document.getElementById('flypig').style.display='block';
window.scrollBy(0,-10); // Every time rose 10px
if(document.body.scrollTop==0) {
document.getElementById('flypig').style.marginBottom=ptt+'px';
ptt=ptt+10;
}
scrolldelay = setTimeout('pageScroll()',2); //2 Millisecond repeat execution 1 time
if(ptt>=1000){
ptt=0;
document.getElementById('flypig').style.marginBottom=20+'px';
document.getElementById('flypig').style.display='none';
clearTimeout(scrolldelay);
}
//pageScroll ends
}
html
<!DOCTYPE html>
<html>
<head>
<title> Returns the implementation of the top button </title>
<meta charset="utf-8" />
<style>
body{
margin:0px;
}
#flypig{
display: none;
float: left;
position: fixed;
bottom: 20px;
margin-left: 75%;
cursor: pointer;
margin-bottom: 20px;
}
#go{
display: block;
float: left;
position: fixed;
bottom: 10px;
margin-left: 75%;
cursor: pointer;
}
</style>
<script>
window.onscroll = function(){
var t = document.documentElement.scrollTop || document.body.scrollTop;
var top_div = document.getElementById( "go" );
if( t >= 300 ) {
top_div.style.display = "block";
} else {
top_div.style.display = "none";
}
}
var ptt=20;
function pageScroll() {
document.getElementById('flypig').style.display='block';
window.scrollBy(0,-10); // Every time rose 10px
if(document.body.scrollTop==0) {
document.getElementById('flypig').style.marginBottom=ptt+'px';
ptt=ptt+10;
}
scrolldelay = setTimeout('pageScroll()',2); //2 Millisecond repeat execution 1 time
if(ptt>=1000){
ptt=0;
document.getElementById('flypig').style.marginBottom=20+'px';
document.getElementById('flypig').style.display='none';
clearTimeout(scrolldelay);
}
//pageScroll ends
}
</script>
</head>
<body>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<h1 style="text-align:center;"> Please slipping </h1>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br /><br />
<br />
<br />
<br />
<br /><br />
<br />
<br />
<br />
<br /><br />
<br />
<br />
<br />
<br /><br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br /><!-- Make your pages long enough -->
<div style="display: none; float: left; position: fixed; margin-left: 50%; cursor: pointer; margin-bottom: 20px;" id="flypig">
<img src="/wp-content/themes/Jakesoft/images/flypig.gif"> </div>
<div style="display: none; float: left; position: fixed; bottom: 10px; margin-left: 50%; cursor: pointer;" id="go" onclick="pageScroll()"> Return to the top </div></html>
This is the end of this article, I hope you enjoy it.