已经是深秋了,森林里那一望无际的林木都已光秃,让褐色的苔掩住它身上的皱纹。无情的秋天剥下了它们美丽的衣裳,它们只好枯秃地站在那里。
本文实例为大家分享了JS返回顶部实例代码,供大家参考,具体内容如下
html/css部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="返回顶部效果.js"></script>
<style>
.container{
width:1190px;
margin: 0px auto;
}
.container a{
display: none;
width:40px;
height:40px;
background: url(/535e0dc100010e9c00400080.jpg) no-repeat;
position: fixed;
left:95%;
bottom: 50px;
}
.container a:hover{
background: url(/535e0dc100010e9c00400080.jpg) no-repeat;
background-position: left -40px;
}
</style>
</head>
<body>
<div class="container">
<img src="/535e0ce800015b7511902787.jpg" alt="">
<a id="btn" rel="nofollow noopener noreferrer" href="javascript:" class="btn" title="回到顶部"></a>
</div>
</body>
</html>
JS部分
window.onload=function(){
var obtn=document.getElementById("btn");
var clientHeight=document.documentElement.clientHeight||ocument.body.clientHeight;
var isTop=true;
var timer=null;
window.onscroll=function(){
var topH=document.documentElement.scrollTop||document.body.scrollTop;
if(topH>clientHeight){
obtn.style.display="block";
}else{
obtn.style.display="none";
}
}
obtn.onclick=function(){
timer=setInterval(function(){
var topH=document.documentElement.scrollTop||document.body.scrollTop;
var stepLength=Math.ceil(topH/5);
document.documentElement.scrollTop=document.body.scrollTop=topH-stepLength;
if(topH==0){
clearInterval(timer);
}
},30);
}
}
学习视频地址
到此这篇关于JS返回顶部实例代码就介绍到这了。笑看人生峰高处,唯有磨难多正果。更多相关JS返回顶部实例代码内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!




