vue 滚动目录树

要做这样一个目录树,先确定一下它的功能

1、点击目录按钮的时候,蓝点滑到点击的位置

2、页面滚动到点击按钮所对应的位置

3、页面滚动时,目录树的蓝点随着滚动条滚动

第一个功能,用css来写 就可以完成

index部分

 <div class="catalog">
     <div class="point">
         <span></span>
             <p :style="{top:point+'px'}">
                <i></i>
             </p>
      </div>
      <ul >
          <li v-for="(item,index) in lists"  :key="index" @click="f_lis(index)">
               {{item.name}}
            </li>
        </ul>
</div>

css部分

.catalog{
        position: fixed;
        display: flex;
        height: 500px;
        width: 10%;
        bottom: 50px;
        right: 2%;
        ul{
            list-style: none;
            color: #999;
        }
        li{
            padding: 15px 10px;
            font-size: 15px;
            cursor: pointer;
        }
        .point{
            position: relative;
            padding: 0 10px;
            span{
                display: block;
                width: 1px;
                height: 450px;
                background: #ccc;
            }
            p{
                position: absolute;
                left: -1px;
                top: 13px;
                padding: 5px;
                background: #d0e2f6;
                box-sizing: border-box;
                border-radius: 50%;
                border: 1px solid #fff;
                transition: all 0.2s linear;
            }
            i{
                display: block;
                width: 12px;
                height: 12px;
                border-radius: 50%;
                background: #0079fe;
            }
        }

所用到的data数据

 lists:[
                {
                   target:"basic",
                   name:"基本信息" 
                },
                {
                   target:"synthesis",
                   name:"综合情况" 
                },
                {
                   target:"bazaar",
                   name:"地理位置" 
                },
                {
                   target:"optimization",
                   name:"个人优点" 
                },
                {
                   target:"preferences",
                   name:"生活喜好" 
                },
                {
                   target:"dongguan",
                   name:"个人荣誉" 
                },
                {
                   target:"overseas",
                   name:"生平事迹" 
                },
                {
                   target:"after",
                   name:"售后服务" 
                },
                // {
                //    target:"basic",
                //    name:"作品集" 
                // },
            ]

这个时候点击目录的时候,蓝点已经可以跟随移动了,这就可以开始第二个功能了,让页面跟着点击的移动

f_lis(i,scroll=false){
            this.point=this.state+i*50
            if(!scroll){
                this.scroll=false
                document.getElementsByClassName('anchor')[i].scrollIntoView({behavior:'smooth',block:'start',inline:'start'});
                setTimeout(()=>{
                    this.scroll=true
                },1000)
            }
        },

第三个功能

  mounted(){
        this.lists_arr=[]
        for(let a=0;a<this.lists.length;a++){
            this.lists_arr.push(
                this.$refs['list'+a].$el.getBoundingClientRect().height
            )
        }
        console.log(this.lists_arr);
    },
watch:{
        index(newVal,old){
            if(this.scroll){
                this.f_lis(newVal,true)
            }
        }
    },
methods:{
     getScroll(event){
            this.lists.forEach((item,index) => {
                let top=this.$refs[item.target].getBoundingClientRect().top
                if(top<0 && Math.abs(top)<this.lists_arr[index]){
                    this.index=index
                }
            })
        },

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qing_xing_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值