Vue3 tab switch切换效果 动态计算tab宽度

在这里插入图片描述

<template>
  <div
      class="tab-list"
      :style="{ '--translatex': translatex, '--width': width }"
    >
      <div
        v-for="(item, index) in type"
        :key="index"
        @click="toogleSelect(index)"
        class="tab-list-item"
        ref="tabRef"
      >
        {{ item.name }}
      </div>
    </div>
</template>

<script setup>
import { computed, onMounted, ref, watch } from "vue";
const translatex = ref("0px"); // 滑块滑动位置
const width = ref("0px"); // 滑块宽度
const tabRef = ref(); //tabitem refs
const activeIndex = ref(0); // 切换下标
const type = [{ name: "光标与点按" }, { name: "滚动与缩放" }, { name: "更多手势" }];
// 计算所有tabitem 宽度数组
const widths = computed(() => {
  let list = [];
  for (const i in tabRef.value) {
    list.push(tabRef.value[i].offsetWidth);
  }
  return list;
});
// 观察切换下标
watch(
  () => activeIndex.value,
  (val) => {
     // 计算滑块的滚动距离
    let totalWidth = widths.value.reduce((pre, cur, index) => {
      if (index > val - 1) {
        return pre + 0;
      }
      return pre + cur;
    }, 0);
    translatex.value = totalWidth + "px";
  }
);
// tab点击
const toogleSelect = (index) => {
  width.value = widths.value[index] + "px";
  activeIndex.value = index;
};
onMounted(() => {
  // 初始化第一个激活tab的宽度
  width.value = widths.value[activeIndex.value] + "px";
});
</script>

<style lang="less">
.tab-list {
    height: 28px;
    border-radius: 10px;
    background: #f3f3f3;
    border: 1px solid #d9d9d9;
    display: flex;
    &-item {
      height: 26px;
      border-radius: 10px;
      line-height: 26px;
      padding: 0 15px;
      text-align: center;
      cursor: pointer;
      z-index: 1;
      position: relative;
    }
    &::after {
      content: "";
      position: absolute;
      background-color: #ffffff;
      border-radius: 10px;
      width: var(--width);
      height: 26px;
      transform: translateX(var(--translatex));
      transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
      z-index: 0;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);;
    }
  }
 </style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值