鱼C论坛

 找回密码
 立即注册
查看: 3511|回复: 3

[已解决]为何这个返回顶部没有正常移动出来

[复制链接]
发表于 2022-4-16 22:18:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 小黄练编程 于 2022-4-16 22:20 编辑
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Document</title>
  8.     <style>
  9.         .topback{
  10.         position:fixed;
  11.         bottom:50%;
  12.         right:0;
  13.         background-color:green;
  14. }
  15. .topback .toppback {
  16.         position:absolute;
  17.         top:0;
  18.         left:0;
  19.         width:160px;
  20.         height:50px;
  21.     text-align:left;
  22.         line-height:50px;
  23.         background-color:green;
  24.         z-index:-1;
  25. }
  26. .topback i{
  27.         display:inline-block;
  28.      width:50px;
  29.          height:50px;
  30.          text-align:center;
  31.          line-height:50px;
  32.          background-color:green;
  33.          cursor:pointer;
  34. }
  35.     </style>
  36. </head>
  37. <body>
  38.     <div class="topback">
  39.         <span><a href="#"><i>&#59941;</i></a></span>
  40.        <div class="toppback"><a href="#">返回顶部</a></div>
  41.    </div>
  42.    <script src="/js/index.js"></script>
  43.     <script>
  44.         function animate( obj, target, callback){
  45.         clearInterval(obj.timer);
  46.          obj.timer=setInterval(function(){
  47.                  var step=(target-obj.offsetLeft)/10;
  48.                  step=step>0?Math.ceil(step):Math.floor(step);
  49.           if(obj.offsetLeft>=target){
  50.                           clearInterval(obj.timer);
  51.                           if(callback){
  52.                                   callback();
  53.                           }
  54.                   }
  55.                   obj.style.left=obj.offsetLeft+step+'px';
  56.         },30);
  57. }
  58.                    var topback=document.querySelector('.topback');
  59.                    var toppback=document.querySelector('.toppback');
  60.               topback.addEventListener('mouseenter',function(){
  61.                                   animate(toppback,-140);
  62.                           });
  63.     </script>
  64. </body>
  65. </html>
复制代码
最佳答案
2022-4-16 22:35:13


offsetLeft 偏移量初始为 0,之后递减为 -140,在递减过程  始终大于你的 target = -140 导致 一直满足 if 条件,进入 if 代码卡后定时器被取消 之后又 回调所以不正常

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-16 22:35:13 | 显示全部楼层    本楼为最佳答案   


offsetLeft 偏移量初始为 0,之后递减为 -140,在递减过程  始终大于你的 target = -140 导致 一直满足 if 条件,进入 if 代码卡后定时器被取消 之后又 回调所以不正常

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-17 17:49:17 | 显示全部楼层
Twilight6 发表于 2022-4-16 22:35
offsetLeft 偏移量初始为 0,之后递减为 -140,在递减过程  始终大于你的 target = -140 导致 一直满足 ...

解决办法是啥,我把>=target改成了=,也没用啊,如何解决啊,一样是输入负的数字。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-17 17:51:40 | 显示全部楼层
Twilight6 发表于 2022-4-16 22:35
offsetLeft 偏移量初始为 0,之后递减为 -140,在递减过程  始终大于你的 target = -140 导致 一直满足 ...

我擦突然忘了,是两个等于号才对,我刚改了,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-5 01:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表