pdd项目之tabbar选项卡

做出来的效果
!(https://upload-images.jianshu.io/upload_images/5617399-976eaf537a26552d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

1.把项目所需要的图标都上传到static文件夹的img文件夹下

因为这个文件夹都是存放一些项目静态资源
因为这个选项卡不是路由组件,是非路由组件,因此在src下的components文件夹下新建一个TabBar
文件夹,里面新建一个名为TabBar.vue的非路由组件

TabBar组件部分的代码

<template>
    <div class="bottom-tab">
        <span class="tab-item"  @click="switchto('/home')">
>
            <img :src="$route.path.includes('/home')?TabBarArray[0].selected:TabBarArray[0].normal" alt="">
            <span :class="{on: $route.path.includes('/home')}">首页</span>
        </span>
        <span class="tab-item" @click="switchto('/recommend')">
            <img :src="$route.path.includes('/recommend')?TabBarArray[1].selected:TabBarArray[1].normal" alt="">
            <span :class="{on: $route.path.includes('/recommend')}">推荐</span>
        </span>
        <span class="tab-item" @click="switchto('/search')">
            <img :src="$route.path.includes('/search')?TabBarArray[2].selected:TabBarArray[2].normal" alt="">
            <span :class="{on: $route.path.includes('search')}">搜索</span>
        </span>
        <span class="tab-item" @click="switchto('/chat')">
            <img :src="$route.path.includes('/chat')?TabBarArray[3].selected:TabBarArray[3].normal" alt="">
            <span :class="{on: $route.path.includes('/chat')}">聊天</span>
        </span>
        <span class="tab-item" @click="switchto('/me')">
            <img :src="$route.path.includes('/me')?TabBarArray[4].selected:TabBarArray[4].normal" alt="">
            <span :class="{on: $route.path.includes('/me')}">个人中心</span>
        </span>
    </div>
</template>

<script>
    export default{
        name:"TabBar",
        data(){
            return {
                    TabBarArray:[
                {normal:require('./../../../static/img/icon_home.png'),selected:require('./../../../static/img/icon_home_selected.png')},
                {normal:require('./../../../static/img/icon_intro.png'),selected:require('./../../../static/img/icon_intro_selected.png')},
                {normal:require('./../../../static/img/icon_search.png'),selected:require('./../../../static/img/icon_search_selected.png')},
                {normal:require('./../../../static/img/icon_chat.png'),selected:require('./../../../static/img/icon_chat_selected.png')},
                {normal:require('./../../../static/img/icon_mine.png'),selected:require('./../../../static/img/icon_mine_selected.png')}
            ]
            }
        },
        methods:{
            switchto(path){
                this.$router.replace(path);
            }
        }
    }


</script>

<style scoped lang="stylus" ref="stylesheet/stylus">
    .bottom-tab
        width 100%
        height 50px
        background-color #fff
        position fixed
        left 0
        bottom 0
        display flex 
        z-index 999
        
        .tab-item
            display flex
            flex 1
            flex-direction column
            align-items center
            justify-content center
            font-size 14px
            color #666
            
            img 
                width 35%
                margin-bottom 2px
            .on
                color red
                
</style>

主组件部分

<template>
    <div id="app">
        <router-view></router-view>
        <tab-bar></tab-bar>
    </div>
</template>

<script>

    import TabBar from './components/TabBar/TabBar'
    export default{
        name:"App",
        components:{
            TabBar
        }
    }


</script>
<style scoped lang="stylus" ref="stylesheet/stylus">
    #app
        width 100%
        height 100%
        background-color #f5f5f5
</style>

2.路由跳转方法解释

给图标均绑上一个swithto的方法,里面的参数即为路由的路径,而且配置完成路由后,增加了两个方法可供使用,router,route
当我们打印this.$router时

微信截图_20181108013352.png

我们需要调用里面的replace方法来切换路由从而达到切换页面的目的

3.选项卡中图片和字体颜色切换方法解释

在Vuetools工具中,我们发现TabBar选项卡是这样的


微信截图_20181108013854.png

我们可以把Path当作是一个突破口,比如当我们选中首页时,路径就为home,当路由地址和path中的地址一致的时候即为选中状态,之所以会用上includes()方法的原因是,之后我们会制作头部二级路由切换,那么路径就会变成/home/hot诸如此类,但是实际上我们依旧还是在home这个路由上,所以只要路径中包含/home即可以视为选中状态,图片则是,先把所需要用到的图片路径放在data数据项中保存,而图片是在本地,所以需要用到require方法把图片路径导入进来

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,107评论 19 139
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,056评论 3 119
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,314评论 25 709
  • 打下这个标题,觉得有些小女人的矫情,但一想,这倒与“奶茶”的文艺范儿有一搭。在娱乐圈里,像“奶茶”这样的女人,读书...
    安若素雪阅读 5,118评论 6 19
  • 我从小就糗事百出,上大学时也不例外。大学时班主任不像高中时那样整天晃的让人讨厌,他们经常是一周才来转一次,更何况我...
    暮瞬阅读 1,261评论 0 0

友情链接更多精彩内容