制作手机app底部导航栏,自带点击特效

文章展示了如何在Vue项目中创建一个自定义的Tabbar组件,包括Tabbar和TabbarItem子组件。TabbarItem具有激活状态判断和点击事件处理,同时实现了消息提醒功能,动态显示未读消息数量。组件样式和功能可定制,适配不同页面路径。

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

 直接在vue项目封装成一个组件

在components目录下创建tabbar文件夹,然后创建Tabbar.vue和TabbarItem.vue两个组件

Tabbar.vue内容为下

<template>
  <div id="tab-bar">
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: "Tabbar"
}
</script>

<style scoped>
#tab-bar {
  display: flex;
  background-color: #f6f6f6;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  box-shadow: 0 -3px 10px rgba(100, 100, 100, .2);
}
</style>

TabbarItem.vue内容为下

<template>
  <div class="tab-bar-item" @click="itemClick">
    <div v-if="!isActive" slot="item-icon">
      <slot name="item-icon"></slot>
    </div>
    <div v-else slot="item-icon-active">
      <slot name="item-icon-active"></slot>
    </div>
    <div slot="item-text" :style="activeStyle">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabbarItem",
  props: {
    path: String,
    activeColor: {
      type: String,
      default: "red",
    },
  },
  data() {
    return {};
  },
  computed: {
    isActive() {
      return this.$route.path.indexOf(this.path) !== -1;
    },
    activeStyle() {
      return this.isActive ? { color: this.activeColor } : { color: "#969595" };
    },
  },
  methods: {
    itemClick() {
      this.$router.replace(this.path);
    },
  },
};
</script>

<style scoped>
.tab-bar-item {
  flex: 1;
  text-align: center;
  height: 82px;
  font-size: 14px;
}

.tab-bar-item img {
  margin: 0 auto;
  margin-top: 4px;
  width: 28px;
  height: 28px;
  /* 去掉图片与文字之间的空隙 */
  vertical-align: middle;
  margin-bottom: 2px;
}

.tab-bar-item img:active {
  transform: scale(1.2);
}
</style>

在components目录下创建MainTabBar.vue组件,内容如下

<template>
  <div>
    <tab-bar>
      <tab-bar-item path="/home" activeColor="#EE9351">
        <img slot="item-icon" src="@/assets/tabIcon/home.png" />
        <img slot="item-icon-active" src="@/assets/tabIcon/home-active.png" />
        <div slot="item-text">首页</div>
      </tab-bar-item>
      <tab-bar-item path="/message" activeColor="#EE9351">
        <van-badge
          slot="item-icon"
          :content="$store.state.messageNum"
          max="99"
          v-if="$store.state.messageNum > 0"
        >
          <img class="badge-inner-img" src="@/assets/tabIcon/message.png" />
        </van-badge>
        <img
          slot="item-icon"
          src="@/assets/tabIcon/message.png"
          v-if="$store.state.messageNum == 0"
        />
        <van-badge
          slot="item-icon-active"
          :content="$store.state.messageNum"
          max="99"
          v-if="$store.state.messageNum > 0"
        >
          <img
            class="badge-inner-img"
            src="@/assets/tabIcon/message-active.png"
          />
        </van-badge>
        <img
          slot="item-icon-active"
          src="@/assets/tabIcon/message-active.png"
          v-if="$store.state.messageNum == 0"
        />
        <div slot="item-text">消息</div>
      </tab-bar-item>
      <tab-bar-item path="/myself" activeColor="#EE9351">
        <img slot="item-icon" src="@/assets/tabIcon/main.png" />
        <img slot="item-icon-active" src="@/assets/tabIcon/main-active.png" />
        <div slot="item-text">我的</div>
      </tab-bar-item>
    </tab-bar>
  </div>
</template>

<script>
import { selectUnreadCount } from "@/api/publicApi";
import TabBar from "./tabbar/Tabbar";
import TabBarItem from "./tabbar/TabbarItem";
export default {
  name: "MainTabBar",
  components: {
    TabBar,
    TabBarItem,
  },
  data() {
    return {
      myTarget: null,
    };
  },
  mounted() {
    selectUnreadCount().then((res) => {
      this.$store.commit("setData", {
        messageNum: res.data,
      });
    });
    this.myTarget = setInterval(() => {
      selectUnreadCount().then((res) => {
        this.$store.commit("setData", {
          messageNum: res.data,
        });
      });
    }, 60 * 1000);
  },
  methods: {},
  beforeDestroy() {
    clearInterval(this.myTarget);
  },
};
</script>

<style scoped>
.badge-inner-img {
  margin: 0 auto;
  width: 24px;
  height: 24px;
  vertical-align: middle;
}
</style>

注意,消息做了消息提醒的红点点,图片换成自己项目中的资源,这样点击自带特效的底部导航栏就做好了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值