tab横向滚动

方法使用:

import BScroll from 'better-scroll';
  1. 引入 import BScroll from ‘better-scroll’; 横向滑动
  2. html 一共有三层
  3. li必须加 flex: none;
    例:
<div ref="tab" class="tab">
            <ul class="ulTab" ref="tabWrapper">
                <li v-for="item in itemList" ref="tabitem" :key="item.id" @click="toggle(item.id)" :class="{red: item.id===active}">
                    {{item.title}}
                </li>
            </ul>
        </div>

3.css
例"

.tab {
            margin-bottom: 15px;
            background: #fff;
            width: 100%;
            height: 40px;
            overflow: hidden;
            /* border-bottom: 1px solid #ccc; */
            top: 52px !important;
            margin-bottom: 15px;
            .ulTab {
                height: 100%;
                display: flex;
                line-height: 2rem;
                border-bottom: 1px solid #ccc;
                border-top: 1px solid #ccc;
                overflow: hidden;
                height: 100%;
                li {
                    height: 100%;
                    height: 100%;
                    box-sizing: border-box;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    flex: none;
                    width: 90px;
                    /* height: 36px; */
                    list-style: none;
                    font-size: 15px;
                    border-left: 1px solid #ccc;
                }
            }
        }"

4.methods:
例:

InitTabScroll() {
                let width = 0
                for (let i = 0; i < this.itemList.length; i++) {
                    width += this.$refs.tabitem[0].getBoundingClientRect().width; //getBoundingClientRect() 返回元素的大小及其相对于视口的位置
                }
                this.$refs.tabWrapper.style.width = width + 'px';
                this.$nextTick(() => {
                    if (!this.scroll) {
                        this.scroll = new BScroll(this.$refs.tab, {
                            startX: 0,
                            click: true,
                            scrollX: true,
                            scrollY: false,
                            bounce: false,
                            eventPassthrough: 'vertical'
                        });
                    } else {
                        this.scroll.refresh()
                    }
                });
            },
  1. mounted
    例:
this.$nextTick(() => {
                this.InitTabScroll();
            });
            ```

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值