vue2/3 tab栏切换 可左右滑动的处理

<template>
 <div class="lose posr">
      <!-- <span class="af"></span> -->
    <div class="tab-layout" id="scroller">
      <ul v-for="(tab, idx) in tabList" :key="idx" class="ul">
        <li
          class="tab-item"
          @click="onClickTab(tab, idx)"
        >
          <span :class="(curTabIndx == idx ? 'active':'')">{{ tab.text }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { defineComponent, computed, toRefs, reactive } from 'vue'
import { jsBridge } from "@/utils/jsbridge";
export default defineComponent({
  name: 'Gooddetails',
  setup() {
    const lData = reactive({
      tabList: [],
      curTabIndx: 0
    })
    const curTab = computed(() => {
      return lData.tabList.find(v => v.select);
    })
    const _init = ()=> {
      let list = ["京东","淘宝/天猫/饿了么","美团外卖","拼多多"];
        list.forEach((text, idx) => {
          lData.tabList.push({
                text,
                id: idx, // tab标识
                select: idx == 0, // 是否被选择
                index: idx // 处于显示的位置
            });
        });
    }
    _init()
       const onClickTab = (tabInfo, idx) => {
          lData.curTabIndx = idx
            let curTabInfo = curTab.value;
            if (curTabInfo.id == tabInfo.id) return;
            let { index, id } = tabInfo;
            // 滑动控件
            let scroller = document.getElementById("scroller");
            let speed = scroller.scrollWidth / lData.tabList.length;
            let tab = document.getElementById(`tab-${id}`);
            let bWidth = document.body.clientWidth;
            // 点击右边
            if (curTabInfo.index < index && tab.clientWidth * index >= bWidth - speed) {
            // 滑动的距离
                scroller.scrollLeft = (index + 2) * speed - bWidth;
            } else if (curTabInfo.index > index && (tab.clientWidth * index - (scroller.scrollLeft + bWidth) < speed)) {
            // 滑动的距离
                scroller.scrollLeft = (index - 1) * speed;
            }

            curTabInfo.select = false;
            lData.tabList[index].select = true;
        }
    return { ...toRefs(lData), curTab, onClickTab }
  }

})
</script>

<style lang="scss" >
@import "../../assets/styles/common.css";

.lose {
    width: 100%;
    position: relative;
    overflow:hidden;
    .af{
      width: .48rem;
      height: .4rem;
      background-color: #f7f7f7;
      position: absolute;
      top: 0.32rem;
      left: 0;
      z-index: 2;
    }
  .tab-layout {
    overflow-x: scroll;
    display: flex;
    flex-wrap: nowrap;
    padding: .32rem 0 .28rem .48rem;
    position: relative;
    .ul:first-child{
        .tab-item{
          padding-left: 0;
        }
      }
    .tab-item {
      // min-width: 1rem;
      padding:0 .32rem;
      text-align: center;
      height: .4rem;
      font-size: .28rem;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #333333;
      line-height: .4rem;
      white-space: nowrap;
      span.active {
        border-bottom:1px solid red;
      }
     
    }
  }
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值