返回顶部

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Javascript 返回顶部</title>
<style type="text/css">
#btn {width:40px; height:40px; position:fixed; right:65px;display: none; bottom:10px; background:url(img/top_bg.png) no-repeat left top;}
#btn:hover {background:url(img/top_bg.png) no-repeat 0 -39px;}
.bg {width:1190px; margin:0 auto;}
</style>

</head>
<body>
<a href="javascript:;" id="btn" title="回到顶部"></a>
<div class="bg">
<img src="img/tb_bg.jpg" alt="" />
</div>
</body>
<script type="text/javascript">
window.onload=function(){

var obtn=document.getElementById("btn");
var timer=null;
var isTop=true;
var clientHeight=document.documentElement.clientHeight || document.body.clientHeight;//可视区域高度
//窗口滚动事件
window.onscroll=function(){
var osTop=document.documentElement.scrollTop || document.body.scrollTop;//滚动条距离顶部的距离
if(osTop>=clientHeight){
obtn.style.display="block"; //返回顶部按钮的显示和隐藏
}
else{
obtn.style.display="none";
}
if(!isTop){
clearInterval(timer); //滚动停止事件
}
isTop=false;

}
obtn.onclick=function(){
//设置定时器
timer=setInterval(function(){
//获取滚动条距离顶部的高度
var osTop=document.documentElement.scrollTop || document.body.scrollTop;
var ispeed=Math.floor(-osTop/5);
document.documentElement.scrollTop=document.body.scrollTop=osTop+ispeed; //滚动条加速效果
isTop=true;
if(osTop==0){
clearInterval(timer);
}
},30);
}
}
</script>
</html>

posted @   不再犯错  阅读(169)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示