Vue 的TabBar点击 实现照片和页面切换

1、本地模拟数据 建议不要用@来进行引入,直接调用本地的地址,即 ../../assets/.../.../...png

<template>
  <div class="tab-bar">
    <template v-for="(item, index) in tabbarData" :key="index">
      <div class="tab-bar-item" 

             <!-- 文字颜色的变化 -->
           :class="currentIndex===index? 'active': ' '"

             <!-- 事件点击 来切换路由和当前的index-->
           @click="itemClick(item,index)"  >

           <!-- 直接使用if 来进行渲染不同的照片 实现切换效果 -->
           <!-- 因为使用的vite来进行开发 所以需要对静态本地照片进行处理 -->

          <img v-if=" currentIndex !== index " :src="getNewURL(item.image)"  />

          <img v-else :src="getNewURL(item.imageActive)"  />

          <span>{{ item.text }}</span>

      </div>
    </template>
  </div>
</template>

<script setup>
import { getNewURL } from "@/utils/getAssertImage";
import { ref } from "@vue/reactivity";

import { useRouter } from "vue-router";
const router = useRouter();
// 动态记录目前点击的位置
let currentIndex = ref(0)

const tabbarData = [
  {
    text: "首页",
    image: "tabbar/tab_home.png",
    imageActive: "tabbar/tab_home_active.png",
    // 路由跳转
    path: "/home",
   
  },
  {
    text: "收藏",
    image: "tabbar/tab_favor.png",
    imageActive: "tabbar/tab_favor_active.png",
    // 路由跳转
    path: "/favor",
  
  },
  {
    text: "订单",
    image: "tabbar/tab_order.png",
    imageActive: "tabbar/tab_order_active.png",
    // 路由跳转
    path: "/order",
  
  },
  {
    text: "消息",
    image: "tabbar/tab_message.png",
    imageActive: "tabbar/tab_message.png",
    // 路由跳转
    path: "/message",
    
  },
];

  function itemClick(item,index){

    currentIndex.value = index

    router.push(item.path);

  }

</script>

<style  scoped>
.tab-bar {
  display: flex;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;

  border-top: 1px solid #f3f3f3;
}
.tab-bar-item {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;

  /* 纵向排列 */
  flex-direction: column;
}
.tab-bar-item img {
  width: 32px;
}
.tab-bar-item span {
  margin-top: 3px;
  font-size: 12px;
}
.active{
  color: #FF9645;
}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值