vant详情页索引显示和隐藏

3 篇文章 0 订阅

在这里插入图片描述
在这里插入图片描述

<template>
    <div class="content">
        <div id="shareTop">
            <div>
                <img src="@/assets/imgs/myBg.png" style="width:100%" alt="">
            </div>
        </div>
        <van-index-bar :index-list="indexList" :sticky-offset-top="50" @select="select" :z-index="zIndex">
            <div v-for="(item,index) of indexList" :key="index">
                <van-index-anchor class="anchor" :index="item"/>
                <p style="text-align: left;">{{item}}</p>
                <van-cell v-for="i of 15" :key="i" :title="'文本'+i" />
            </div>
        </van-index-bar>
    </div>
</template>

<script>
export default {
    data() {
        return {
            indexList: ["产品介绍", "投保须知", "服务约定", "理赔流程"],
            height:200,//索引内容区域距离顶部的高度
            zIndex:"-1",
        };
    },
    mounted(){
        setTimeout(()=>{
            let height=document.querySelector(".van-index-bar").offsetTop;
            this.height=height-50;
        },10)
        window.addEventListener('scroll',this.scrollPosition);
    },
    destroyed() {     //销毁
        window.removeEventListener("scroll",this.scrollPosition);
    },
    methods:{
        scrollPosition(){
            let scrollTop=document.documentElement.scrollTop || document.body.scrollTop//滚动条滚动时,距离顶部的距离
            console.log(scrollTop,this.height)
            if(scrollTop>=this.height && this.zIndex!="1"){
                this.zIndex="1";
                return;
            }else if(scrollTop<this.height && this.zIndex!="-1"){
                this.zIndex="-1";
                return;
            }
        },
        select(e){
            console.log(e,"sssssssssssss");
            document.body.scrollTop +=1;
            document.documentElement.scrollTop +=1;
        },
        change(e){
            console.log(e,"eeeeeeeeee")
        }
    },
    watch:{
    }
}
</script>
<style scoped>
    .content /deep/ .van-index-bar__sidebar{
        top: 0;
        transform: translateY(0);
        left: 0;
        right: 0;
        flex-direction: row;
        justify-content: space-between;
        background: #fff;
    }
    .content /deep/ .van-index-bar__index{
        height: 50px;
        line-height: 50px;
        flex: auto;
        padding: 0;
    }

    .content /deep/ .van-index-bar__index--active{
        background: #f00;
        color: #fff;
    }
    .content /deep/ .van-index-anchor--sticky,.content /deep/ .van-index-anchor{
        opacity: 0; 
    } 
    .anchor{
        height: 0 !important;
    }
</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值