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

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

 直接在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>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用vue-router和vue-awesome-swiper第三方库来实现一个带有底部导航栏手机app界面。 首先,在main.js中导入vue-router和vue-awesome-swiper: ``` import Vue from 'vue' import VueRouter from 'vue-router' import VueAwesomeSwiper from 'vue-awesome-swiper' import 'swiper/dist/css/swiper.css' Vue.use(VueRouter) Vue.use(VueAwesomeSwiper) ``` 然后,创建一个App.vue组件,包含底部导航栏和路由视图: ``` <template> <div id="app"> <div class="main"> <router-view></router-view> </div> <div class="tabbar"> <router-link to="/" class="tabbar-item"> <i class="iconfont icon-home"></i> <span>首页</span> </router-link> <router-link to="/category" class="tabbar-item"> <i class="iconfont icon-category"></i> <span>分类</span> </router-link> <router-link to="/cart" class="tabbar-item"> <i class="iconfont icon-cart"></i> <span>购物车</span> </router-link> <router-link to="/profile" class="tabbar-item"> <i class="iconfont icon-profile"></i> <span>我的</span> </router-link> </div> </div> </template> <script> export default { name: 'App' } </script> <style> .tabbar { display: flex; position: fixed; bottom: 0; left: 0; width: 100%; background-color: #fff; box-shadow: 0 -2px 4px rgba(0,0,0,.15); z-index: 99; } .tabbar-item { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #999; font-size: 12px; text-decoration: none; } .tabbar-item.active { color: #33a3f4; } .tabbar-item i { font-size: 20px; margin-bottom: 2px; } .main { margin-bottom: 50px; } </style> ``` 在路由配置文件中,定义每个路由对应的组件: ``` import Vue from 'vue' import Router from 'vue-router' import Home from './views/Home.vue' import Category from './views/Category.vue' import Cart from './views/Cart.vue' import Profile from './views/Profile.vue' Vue.use(Router) export default new Router({ routes: [ { path: '/', component: Home }, { path: '/category', component: Category }, { path: '/cart', component: Cart }, { path: '/profile', component: Profile } ] }) ``` 最后,在各个页面组件中,使用vue-awesome-swiper实现轮播图等效果。 以上就是一个简单的带有底部导航栏手机app界面的实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值