vue自定义移动端底部导航栏


三个步骤
1.在components中内写好第一部分代码
2.新建四个文件用于底部的切换跳转即可
在这里插入图片描述

3.在router配置路径的跳转

一:自定义底部导航条

components中tabbar组件代码如下,
图片路径换成自己的,可以自己添加底部数量

<template>
  <div>
    <div id="tab-bar">
      <div class="tab-bar-item" @click="homeClick()">
        <img v-if="!ishome" src="../../assets/img/tabbar/home.svg" alt="" />
        <img v-else src="../../assets/img/tabbar/home_active.svg" alt="" />
        <div :class="{ active: ishome }">首页</div>
      </div>
      <div class="tab-bar-item" @click="categoryClick()">
        <img
          path="./category"
          v-if="!isActive"
          src="../../assets/img/tabbar/category.svg"
          alt=""
        />
        <img v-else src="../../assets/img/tabbar/category_active.svg" alt="" />
        <div :class="{ active: isActive }">分类</div>
      </div>
      <div class="tab-bar-item" @click="shopcartClick()">
        <img
          v-if="!iscart"
          src="../../assets/img/tabbar/shopcart.svg"
          alt=""
        />
        <img v-else src="../../assets/img/tabbar/shopcart_active.svg" alt="" />
        <div :class="{ active: iscart }">购物车</div>
      </div>
      <div class="tab-bar-item" @click="profileClick()">
        <img
          v-if="!isprofile"
          src="../../assets/img/tabbar/profile.svg"
          alt=""
        />
        <img v-else src="../../assets/img/tabbar/profile_active.svg" alt="" />
        <div :class="{ active: isprofile }">我的</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  computed:{
      isActive(){
          return this.$route.path=="/category"
      },
      ishome(){
           return this.$route.path==("/")
      },
      iscart(){
          return this.$route.path==("/cart")
      },
      isprofile(){
           return this.$route.path==("/profile")
      }
  },
  methods: {
    categoryClick() {
      if (this.$route.path !== "/category") {
        this.$router.replace("/category");
      }
    },
    homeClick() {
      if (this.$route.path !== "/") {
        this.$router.replace("/");
      }
    },
    shopcartClick() {
      if (this.$route.path !== "/cart") {
        this.$router.replace("/cart");
      }
    },
    profileClick() {
      if (this.$route.path !== "/profile") {
        this.$router.replace("/profile");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
#tab-bar {
  display: flex;
  background-color: #f6f6f6;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  box-shadow: 0px -1px 1px rgba(100, 100, 100, 0.2);
}
.tab-bar-item {
  flex: 1;
  text-align: center;
  height: 49px;
  font-size: 14px;
}
.tab-bar-item img {
  width: 24px;
  height: 24px;
  margin-top: 3px;
  vertical-align: middle;
  margin-bottom: 2px;
}
.active {
  color: rgb(243, 74, 102);
}
</style>

路由router配置

index.js

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home/Home.vue')
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import('../views/category/Category.vue')
  }, {
    path: '/cart',
    name: 'Cart',
    component: () => import('../views/cart/Cart.vue')
  }, {
    path: '/profile',
    name: 'Profile',
    component: () => import('../views/profile/Profile.vue')
  },
] 

const router = new VueRouter({
  routes
})

export default router

这样就完成了底部的封装

二、自定义顶部导航条

顶部headerTop组件如下:

<template>
  <div class="nav_bar">
    <div class="left"><slot name="left"></slot></div>
    <div class="center"><slot name="center"></slot></div>
    <div class="right"><slot name="right"></slot></div>
  </div>
</template>

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

<style scoped>
.nav_bar {
  display: flex;
  line-height: 44px;
  text-align: center;
  height: 44px;
  box-shadow: 0 1px 1px rgba(100, 100, 100, .1);
}
.left,
.right {
  width: 60px;
}
.center {
  flex: 1;
}
</style>

组件中的使用

<template>
  <div>
    <header-bar class="home-nav">
      <div slot="center">搭配贴士</div>
    </header-bar>
    <Tabbar />
  </div>
</template>

<script>
import Tabbar from "../../components/TabBar/TabBar.vue";
import HeaderBar from "../../components/Header/Header.vue";
export default {
  components: {
    Tabbar,
    HeaderBar,
  },
};
</script>

<style scoped>
  .home-nav{
    background-color: var(--color-tint);
  }
</style>
### 移动端底部导航栏TabBar)样式实现与设计指南 #### 一、组件选择 对于构建移动端应用程序中的底部导航栏(TabBar),可以采用Vant UI库提供的`Tabbar`组件来创建。此组件能够提供基础的导航功能并支持自定义图标和文字,满足大多数应用场景的需求[^1]。 ```html <template> <van-tabbar v-model="active"> <van-tabbar-item icon="home-o">首页</van-tabbar-item> <van-tabbar-item icon="search-o">搜索</van-tabbar-item> <van-tabbar-item icon="friends-o">朋友</van-tabbar-item> <van-tabbar-item icon="setting-o">设置</van-tabbar-item> </van-tabbar> </template> <script setup> import { ref } from 'vue'; let active = ref(0); </script> ``` #### 二、响应式布局处理 鉴于移动设备种类繁多,屏幕尺寸差异较大,在开发过程中需特别注意页面元素在各种条件下的显示效果一致性。为了确保良好的用户体验,建议采取一套完善的移动端适配策略,比如利用CSS媒体查询针对不同宽度范围调整样式属性,或是借助vw/vh单位使控件大小随视窗变化而自动缩放[^2]。 ```css @media only screen and (max-width: 600px){ .custom-class{ font-size: 14px; } } .tab-bar { height: calc(8vh); /* 使用 vh 单位 */ } ``` #### 三、交互体验优化原则 遵循UI/UX最佳实践,保持操作流程简洁明了,避免不必要的复杂度;同时保证信息传达精准无误,有助于提高用户的理解和接受程度。具体而言: - **简化流程**:通过合理规划菜单结构,减少点击次数; - **清晰明确**:选用易于辨认的文字描述及图形符号作为标签提示; - **帮助用户识别**:固定位置放置常用选项,增强视觉连贯性和可预测性[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值