jquery实现点击返回顶部
未知
2021-06-22 11:49:09
0
html:
 <style type="text/css">
    #gotop{width:0.9rem;height:0.9rem;background:url(../images/gotop.png) no-repeat center top;background-size:0.9rem auto;position: fixed;bottom:20%;right:3%;cursor: pointer;z-index: 10;}
    #gotop span{
     background: gray;font-size: 35px;
    }

 </style>

 <!--侧边栏按钮-->
  <div class="mreturn" id="gotop">
    <a href="javascript:gotoTop()">
        <span class="layui-icon layui-icon-up"></span>
    </a>
</div>

jquery:
function gotoTop(acceleration,stime) {
   acceleration = acceleration || 0.1;
   stime = stime || 10;
   var x1 = 0;
   var y1 = 0;
   var x2 = 0;
   var y2 = 0;
   var x3 = 0;
   var y3 = 0;
   if (document.documentElement) {
       x1 = document.documentElement.scrollLeft || 0;
       y1 = document.documentElement.scrollTop || 0;
   }

   if (document.body) {
       x2 = document.body.scrollLeft || 0;
       y2 = document.body.scrollTop || 0;
   }

   var x3 = window.scrollX || 0;
   var y3 = window.scrollY || 0; 

   // 滚动条到页面顶部的水平距离
   var x = Math.max(x1, Math.max(x2, x3));

   // 滚动条到页面顶部的垂直距离
   var y = Math.max(y1, Math.max(y2, y3)); 

   // 滚动距离 = 目前距离 / 速度, 因为距离原来越小, 速度是大于 1 的数, 所以滚动距离会越来越小
   var speeding = 1 + acceleration;
   window.scrollTo(Math.floor(x / speeding), Math.floor(y / speeding)); 

   // 如果距离不为零, 继续调用函数
   if(x > 0 || y > 0) {
       var run = "gotoTop(" + acceleration + ", " + stime + ")";
       window.setTimeout(run, stime);
   }

}

相关内容

PHP与YAML集成:实现...
本文介绍了PHP与YAML集成的实现方式,包括读取和写入YAML文...
2026-02-03 01:23:45
Tomcat在云服务器中的...
摘要: 本文探讨了Tomcat在云服务器中实现高可用性的方法,包...
2025-11-03 01:23:42
云服务器上MongoDB数...
本文介绍了MongoDB数据库在云服务器上的迁移与扩展策略,包括迁...
2025-08-31 01:23:39
深入理解云服务器Mongo...
本文深入解析了云服务器上MongoDB的集群架构,包括副本集、分片...
2025-08-30 01:23:42
高效管理云服务器上的Mon...
摘要:高效管理云服务器MongoDB数据库需关注安全、性能、数据维...
2025-08-29 01:23:42
如何在云服务器上搭建高可用...
摘要:本文介绍了在云服务器上搭建高可用MongoDB副本集的步骤,...
2025-08-28 01:23:41

热门资讯

downloadable fo... downloadable font: Glyph bbox was incorrect怎么处理
腾讯邮箱qq邮箱使用SMTP开... 第一步:登录 https://mail.qq.com/ 登录后点击设置---账号 第二步:点击【开启...
layui tooltip提示... 在layui表单中,我们想把一个标签做出提示,以标明这个表单元素是做什么有什么功能时,就可以用上提...
layui中出错Unexpec... layui中出错Unexpected identifier
解决Phpcms V9手机门户... 第一步:开启手机网站。位置:模块 》手机门户 》 添加手机站点,如果模块里面没有手机门户,那么就在模...
JavaScript获取当前协... JavaScript获取当前协议,域名,路径
JS实现html打印功能,打印... !DOCTYPE htmlhtmlhead meta charset=utf-8 title打印功能...
bootstrap 图片文字居... 问题描述: 让图片和文字居中显示。 解决办法: !-- style--.navbars img{ m...
bootstrap图片排版布局 问题描述: 在使用bootstrap的时候,出现了这样的问题。 排版对不齐。 解决办法: 通过上图发...