vue手写tab栏切换

本文展示了如何使用Vue.js实现一个动态的标题标签切换组件,配合Less样式,实现在点击时切换背景图片和高亮显示。通过监听`num`值的变化,实现了图片和CSS类的实时更新,适合前端开发者的日常实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下是我的代码 

<template>
  <span v-for="(item, index) in title" :key="index" :class="{ active: num == index, tabs: true }" @click="num = index">
    {{ item }}
  </span>
</template>

<script setup lang="ts">
defineProps<{
  title: string[]
}>()
const num: any = ref('p')
watch(num, (newValue, _oldValue) => {
  // 监听新值用于后期利用这个值将值进行子传父
  console.log(newValue);

})

</script>

<style lang="less" scoped>
.active {
  color: yellow;
}

.tabs {
  cursor: pointer;
  display: inline-block;
  width: 120px;
  height: 50px;
  line-height: 50px;
  text-align: center;
}
</style>

再优化一下加上图片切换

<template>
  <span v-for="(item, index) in title" :key="index" :class="{ active: num == index, tabs: true }" @click="num = index">
    <span :class="{ active1: num == index }">{{ item }}</span>
  </span>
</template>

<script setup lang="ts">
defineProps<{
  title: string[]
}>()
const num: any = ref('p')
watch(num, (newValue, _oldValue) => {
  // 监听新值用于后期利用这个值将值进行子传父
  console.log(newValue);

})

</script>

<style lang="less" scoped>
.active {
  color: yellow;
}

.tabs {
  cursor: pointer;
  display: inline-block;
  width: 120px;
  height: 50px;
  line-height: 50px;
  text-align: center;

  background-image: url(../.../../../../public/R-C111.jpg);
  background-size: 100%;

}

.active1 {
  display: inline-block;
  width: 120px;
  height: 50px;
  background-image: url(../.../../../../public/R-C.jpg);
  background-size: 100%;
}
</style>

最终效果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值