移动端tab导航栏(切换底部条滑动,超过5个居中滑动)

GIF.gif

通过在列表底部加一个div,实现底部滑动条跟随滑动
所有代码

<template>
    <div class="list-content">
        <div class="list-content-hidden">
            <ul
                class="menu-list"
                ref="scrollUl"
            >
                <li
                    class="li"
                    v-for="(item,index) in list"
                    :class="activeIndex == index ? 'active' : ''"
                    @click="checkMenuList(index,item.id)"
                    :id="'menuli'+index"
                >
                    <div :id="'menu'+index">{{item.name}}</div>
                </li>
                <div
                    class="hr"
                    ref="menuHR"
                ></div>
            </ul>
        </div>

    </div>
</template>
<script lang="ts">
import { Component, Mixins, Watch, Prop } from 'vue-property-decorator';
@Component({
})
export default class Tablist extends Mixins() {
    activeIndex = 0;
    list: any = [
        { id: 1, name: '一一' },
        { id: 2, name: '二二' },
        { id: 3, name: '三三' },
        { id: 4, name: '四四' },
        { id: 5, name: '五五' },
        { id: 6, name: '六六' },
        { id: 6, name: '七七' },
        { id: 6, name: '八八' },
        { id: 6, name: '九九' },
    ];
    // 点击切换数据
    checkMenuList(index, id) {
        this.activeIndex = index;
        this.$nextTick(() => {
            // 获取被点击的div,并且把div的宽度给滑动条
            let menuli: any = document.getElementById('menu' + index);
            let liclientWidth = menuli.clientWidth;
            (this.$refs as any).menuHR.style.width = liclientWidth + 'px';
            // 设置滑动条距离左边的距离等于当前div距离左边的距离
            (this.$refs as any).menuHR.style.left = menuli.offsetLeft + 'px';
            // 所有菜单个数
            let total: any = (this.list as any).length;

            let scrollUl = this.$refs.scrollUl;
            // 找到当前选择 和前后2个div 保持这5个一直在可视区域
            let menuliben: any = document.getElementById('menuli' + index);
            let menulipre: any = document.getElementById('menuli' + (index - 1));
            let menulipre2: any = document.getElementById('menuli' + (index - 2));
            let menulinext: any = document.getElementById('menuli' + (index + 1));
            let menulinext2: any = document.getElementById('menuli' + (index + 2));

            // scrollUl.scrollLeft = menuliben.offsetLeft;
            if (index == 0) { // 第一个
                menuliben.scrollIntoView();
            } else if (index == 1) { // 第二个
                menuliben.scrollIntoView();
                menulipre.scrollIntoView();
                menulinext.scrollIntoView();
                menulinext2.scrollIntoView();
            } else if (index == total - 2) { // 倒数第二个
                menuliben.scrollIntoView();
                menulipre.scrollIntoView();
                menulipre2.scrollIntoView();
                menulinext.scrollIntoView();
            } else if (index == total - 1) { // 倒数第一个
                menuliben.scrollIntoView();
                menulipre.scrollIntoView();
                menulipre2.scrollIntoView();
            } else { // 其他
                menuliben.scrollIntoView();
                menulipre.scrollIntoView();
                menulipre2.scrollIntoView();
                menulinext.scrollIntoView();
                menulinext2.scrollIntoView();
            }
        });
    }
}
</script>
<style lang="scss" scoped>
.list-content {
    width: 100%;
}
.list-content-hidden {
    width: 100%;
    padding: 0 0.25rem;
    overflow: hidden;
    height: 0.88rem;
    display: flex;
    align-items: flex-end;
}
.hr {
    width: 0.65rem;
    height: 0.06rem;
    background: var(--theme1);
    position: absolute;
    left: 0.38rem;
    bottom: 0.01rem;
    transition: 0.2s all linear;
    border-radius: 0.03rem;
}
.menu-list {
    white-space: nowrap;
    overflow-x: scroll;
    width: auto;
    position: relative;
    flex: 1;
    &::-webkit-scrollbar {
        display: none;
    }
    .li {
        height: 0.7rem;
        line-height: 0.7rem;
        width: 1.4rem;
        text-align: center;
        display: inline-block;
        font-size: 0.32rem;
        color: rgba(0, 0, 0, 0.9);
        cursor: pointer;
        &.active {
            color: $theme1;
            color: var(--theme1);
            font-weight: bold;
            font-size: 0.36rem;
        }
        div {
            display: inline-block;
        }
    }
}
</style>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 整个三月借着春困的由头睡了许多懒觉,慵懒的一天天,日子似乎也就慢了下来。 下班后不再把另外的工作一项项铺开来,完全...
    白椿耳阅读 254评论 0 1
  • 关于舜帝生于何处,一直存有争议。在山东就有两处,一处是省会济南,一处是潍坊诸城。 在省会济南有舜井街,舜耕山庄等。...
    刘富富阅读 905评论 0 0
  • 1.cement 英英释义:to make a relationship, idea etc stronger o...
    你好Ruby阅读 568评论 0 0
  • 昨天朋友回妈妈家吃饭,看到弟媳妇和侄子都拉着脸,吃完饭,弟媳妇板着脸对侄子说,你一个人先走回去吧,我不想看到你。孩...
    拂尘记阅读 1,123评论 0 5

友情链接更多精彩内容