vue实现头部吸顶描点

vue实现头部吸顶描点

效果:页面滚到具体位置时,顶部吸附(粘性定位),点击描点跳转到对应位置,滚到到对应位置描点自动切换。
在这里插入图片描述
html:使用elment ui,name对应每个位置的ref名称,

 			<el-tabs @tab-click="jump" v-model="tabActive">
                    <el-tab-pane label="基础信息" name="base" ></el-tab-pane>
                    <el-tab-pane label="经营信息" name="manage"></el-tab-pane>
                    <el-tab-pane
                        label="老板及对接人信息"
                        name="boss"
                    ></el-tab-pane>
                    <el-tab-pane label="其它信息" name="other"></el-tab-pane>
                </el-tabs>

举例:基础信息ref:
在这里插入图片描述
点击描点跳转具体位置方法:

 jump(e) {
            // 当前窗口正中心位置到指定dom位置的距离

            //页面滚动了的距离
            let height =
                window.pageYOffset ||
                document.documentElement.scrollTop ||
                document.body.scrollTop;

            //指定dom到页面顶端的距离
            let dom = this.$refs[e.name];
            let domHeight = dom.offsetTop + 60;

            //滚动距离计算
            var S = Number(height) - Number(domHeight);

            //判断上滚还是下滚
            if (S < 0) {
                //下滚
                S = Math.abs(S);
                window.scrollBy({ top: S, behavior: "smooth" });
                 this.$refs.nav.style.position = 'fixed'
                 this.$refs.nav.style.top = '0px'
            } else if (S == 0) {
                //不滚
                window.scrollBy({ top: 0, behavior: "smooth" });
            } else {
                //上滚
                S = -S;
                window.scrollBy({ top: S, behavior: "smooth" });
            }

滚到到对应位置描点自动切换方法:
mouted中添加监听滚动事件:

//给window添加一个滚动滚动监听事件
window.addEventListener('scroll', this.handleScroll)

具体方法

 handleScroll(){
          var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
          console.log(scrollTop)
          if(scrollTop>170){
              this.$refs.nav.style.position = 'fixed'
              this.$refs.nav.style.top = '0px'
              
              if(scrollTop>170 && scrollTop<this.$refs['manage'].offsetTop){
               this.tabActive = 'base'
              }
              else if(scrollTop>this.$refs['manage'].offsetTop && scrollTop<this.$refs['boss'].offsetTop){
               this.tabActive = 'manage'
              }
              else if(scrollTop>this.$refs['boss'].offsetTop  && scrollTop<this.$refs['other'].offsetTop-500){
               this.tabActive = 'boss'
              }
              else if(scrollTop>this.$refs['other'].offsetTop-500 ){
               this.tabActive = 'other'
              }
          }
          else{
            this.$refs.nav.style.position = ''
          }
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值