最好看的TabBar(Vue)

今天跟大家分享一个自己模仿得物的tabbar栏,用Vue写的!用到了router,实现了页面动态的跳转。

目录结构(分了3个vue  template)

TabBar.vue

TabBarItem.vue

MainTabBar.vue

源码

MainTabBar.vue

<template>
    <tab-bar>
        <tab-bar-item path="/home">
            <template v-slot:item-icon><img class="logo-image" src="../../../src/assets/img/logo2.png" alt=""></template>
            <template v-slot:item-icon-active><img class="logo-image" src="../../../src/assets/img/logo2.png" alt=""></template>
        </tab-bar-item>
        <tab-bar-item path="/home" activeColor="mediumturquoise">
            <template v-slot:item-text><div class="title-style">首页</div></template>
            <template v-slot:item-icon><img src="" alt=""></template>
            <template v-slot:item-icon-active><img class="hengxian" src="../../../src/assets/img/hengxian.png" alt=""></template>
        </tab-bar-item>
        <tab-bar-item path="/shop" activeColor="mediumturquoise">
            <template v-slot:item-text><div class="title-style">购物精选</div></template>
            <template v-slot:item-icon><img src="" alt=""></template>
            <template v-slot:item-icon-active><img class="hengxian" src="../../../src/assets/img/hengxian.png" alt=""></template>
        </tab-bar-item>
        <tab-bar-item path="/community" activeColor="mediumturquoise">
            <template v-slot:item-text><div class="title-style">社区精选</div></template>
            <template v-slot:item-icon><img src="" alt=""></template>
            <template v-slot:item-icon-active><img class="hengxian" src="../../../src/assets/img/hengxian.png" alt=""></template>
        </tab-bar-item>
        <tab-bar-item path="/store" activeColor="mediumturquoise">
            <template v-slot:item-text><div class="title-style">商家入住</div></template>
            <template v-slot:item-icon><img src="" alt=""></template>
            <template v-slot:item-icon-active><img class="hengxian" src="../../../src/assets/img/hengxian.png" alt=""></template>
        </tab-bar-item>
        <tab-bar-item path="/create" activeColor="mediumturquoise">
            <template v-slot:item-text><div class="title-style">创作者服务</div></template>
            <template v-slot:item-icon><img src="" alt=""></template>
            <template v-slot:item-icon-active><img class="hengxian" src="../../../src/assets/img/hengxian.png" alt=""></template>
        </tab-bar-item>
        <tab-bar-item path="/about" activeColor="mediumturquoise">
            <template v-slot:item-text><div class="title-style">关于我们</div></template>
            <template v-slot:item-icon><img src="" alt=""></template>
            <template v-slot:item-icon-active><img class="hengxian" src="../../../src/assets/img/hengxian.png" alt=""></template>
        </tab-bar-item>
        </tab-bar>
</template>

<script>
    import TabBar from "./tabbar/TabBar";
    import TabBarItem from "./tabbar/TabBarItem";

    export default {
        name: "MainTabBar",
        components: {
            TabBar,
            TabBarItem
        }
    }
</script>

<style scoped>
    .title-style {
        font-weight: bold;
        position: relative;
        top: 20px;
        cursor: pointer;
    }
    .title-style:hover {
        color: #48decc;
    }

    .logo-image {
        width: 100px;
        position: relative;
        top: 20px;
    }

    .hengxian {
        position: relative;
        top: 23px;
        width: 50px;
        height: 15px;
    }
</style>

TabBarItem.vue

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

<script>
    export default {
        name: "TarBarItem",
        props: {
            path: String,
            activeColor: {
                type: String,
                /*如果没有参数 则这个为默认的颜色*/
                default: '#48decc'
            }
        },
        data() {
           return {
               // isActive: true
           }
        },
        computed: {
          isActive() {
              /*动态决定颜色是否改变*/
              return this.$route.path.indexOf(this.path) !== -1
          },
            activeStyle() {
              return this.isActive ? {color: this.activeColor} : {}
            }
        },
        methods: {
            itemClick() {
                this.$router.push(this.path)
            }
        }
    }
</script>

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

    .tab-bar-item img {

    }

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: rgba(202,203,203,0.09);
        box-shadow: -3px 0 1px rgba(100,100,100,.6);
    }
</style>

其中router跟view的结构

router中的代码(目录记得改)

import { createRouter, createWebHistory } from 'vue-router'

const Home = () => import('../views/home/Home');
const About = () => import('../views/about/About');
const Community = () => import('../views/community/Community');
const Shop = () => import('../views/shop/Shop');
const Store = () => import('../views/store/Store');
const Create = () => import('../views/create/Create');

const routes = [
  {
    path: '',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home
  },
  {
    path: '/about',
    component: About
  },
  {
    path: '/community',
    component: Community
  },
  {
    path: '/shop',
    component: Shop
  },
  {
    path: '/store',
    component: Store
  },
  {
    path: '/create',
    component: Create
  }
];

const router = createRouter({
  // history: createWebHashHistory(),
  history: createWebHistory(process.env.BASE_URL),
  routes,
  mode: 'history'
});

export default router

第一次发,谢谢支持!!!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值