js锚点跳转添加过度动画,vue锚点跳转js添加平滑过度效果

这里声明一下,a标签锚点跳转和本文跳转不冲突,如果不需要平滑效果,只需要点击跳转锚点连接看此文;:vue实现锚点定位跳转,a标签实现锚点定位跳转
先看看最终效果图:

js锚点跳转添加过度动画,id锚点跳转js添加平滑过度效果

接下来看代码:我的导航栏是动态的,内容列表也是循环出来的,其实道理都一样,基本原理就是找到跳转目标元素距离顶部的距离(document.getElementById(‘#type’)).offsetTop),然后获取当前距离顶部距离(document.documentElement.scrollTop),然后用js进行上下滑动操作,接下来看代码,为了看了简介,我删掉了不相关代码:

HTML

<!-- 右侧悬浮导航栏 -->
        <div>
          <div v-for="(item, indext) in fixList" :key="indext">
            <div
              @click="tapItem(indext)"
            >
              {{ item.name}}
            </div>
          </div>
          <div>顶部</div>
        </div>
        
        <!-- 商品分组显示页,也就是跳转的目标页 -->
        <div
          v-for="(item, index) in fixList"
          :key="index"
          :name="'type-' + index"
          :id="'type-' + index"
        >
        	内容区域,视频中的商品列表区....
        </div>

JS

tapItem(index) {
      // 点击单个分类,跳转到相应的锚点连接,并添加过度动画平滑效果
      this.activeBtn = index;
      // 需要跳转的目标锚点所在高度,因为定位不准确,应该是以底部为跳转距离了,我这里添加了一个屏幕高度,跳转位置刚好正确,根据项目自己修改
      let jumpHeight = (document.getElementById('type-' + index)).offsetTop + (document.documentElement.clientHeight - 90); 
      // 当前位置距离顶部的高度
      let top = document.documentElement.scrollTop
      if(jumpHeight > top) { 
        // 目标高度大于当前高度,说明需要往下滑
        let heigth = jumpHeight - top;
        if (this.myTimer == -1) { // 这个是一个定时器,用来防止重复点击的
          //滑动高度大于10000,在0.8秒内完成平移动画,80可以自己根据实际情况来改就是0.8秒
        if (heigth > 10000) {
          let num = Number((heigth / 80).toFixed(0));
          this.myTimer = setInterval(() => {
            top += num;
            if (top >= jumpHeight) {
              top = jumpHeight;
              window.clearInterval(this.myTimer);
              this.myTimer = -1;
            }
            window.scrollTo(0, top);
          }, 10);
        } else {
          // 滑动距离小于10000,按默认速度滑动
          this.myTimer = setInterval(() => {
            top += 80;
            if (top >= jumpHeight) {
              top = jumpHeight;
              window.clearInterval(this.myTimer);
              this.myTimer = -1;
            }
            window.scrollTo(0, top);
          }, 10);
        }
      }
      }else { // 目标高度小于当前高度,说明需要往上滑
        let heigth = top - jumpHeight;
        if (this.myTimer == -1) {
        if (heigth > 10000) {
          let num = Number((heigth / 80).toFixed(0));
          this.myTimer = setInterval(() => {
            top  -= num;
            if (top <= jumpHeight) {
              top = jumpHeight;
              window.clearInterval(this.myTimer);
              this.myTimer = -1;
            }
            window.scrollTo(0, top);
          }, 10);
        } else {
          this.myTimer = setInterval(() => {
            top -= 80;
            if (top <= jumpHeight) {
              top = jumpHeight;
              window.clearInterval(this.myTimer);
              this.myTimer = -1;
            }
            window.scrollTo(0, top);
          }, 10);
        }
      }
      }
    },

学废了点个赞呗!
在这里插入图片描述

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端攻城狮路飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值