uniapp 小程序实现自定义底部导航栏(tarbar)

摘要:
'1rpxsolid'+tabBar.borderStyle:0}“˃˂image:src=“http://t.zoukankan.com/selected==index?

在小程序开发中,默认底部导航栏很难满足实际需求,好在官方给出了自定义形式,效果如下:

uniapp 小程序实现自定义底部导航栏(tarbar)第1张

话不多说,直接上代码

1.组件

custom-tarbar.vue文件

uniapp 小程序实现自定义底部导航栏(tarbar)第2张uniapp 小程序实现自定义底部导航栏(tarbar)第3张
<template>
    <view class="tarbar">
        <view
            class=".tarbar-list"
            :style="{
                background: tabBar.backgroundColor,
                color: tabBar.color,
                'border-top': tabBar.position == 'bottom' ? '1rpx solid ' + tabBar.borderStyle : 0,
                'border-bottom': tabBar.position == 'top' ? '1rpx solid ' + tabBar.borderStyle : 0
            }"
        >
            <view class="tarbar-list-ul">
                <view   :class="index == 2 ? 'tarbar-list-li-center' : ''" v-for="(item, index) in tabBar.list" :key="index" @click.top="setSelected(index)">
                    <block v-if="index != 2">
                        <view class="tarbar-list-li-icon"><image :src="http://t.zoukankan.com/selected == index ? item.selectedIconPath : item.iconPath" mode=""></image></view>
                        <view class="tarbar-list-li-name">{{ item.text }}</view>
                    </block>
                    <block v-else>
                        <view class="tarbar-list-li-icon"><image :src="http://t.zoukankan.com/item.selectedIconPath" mode=""></image></view>
                    </block>
                </view>
            </view>
        </view>
        <block v-if="isShowMask"><release-popup @close-mask="closeMask"></release-popup></block>
    </view>
</template>

<script>
import releasePopup from './release-popup.vue';
export default {
    components: {
        'release-popup': releasePopup
    },
    props: ['selected'],
    data() {
        return {
            tabBar: {
                color: '#ccc',
                selectedColor: '#E84351',
                borderStyle: '#ccc',
                backgroundColor: '#fff',
                position: 'bottom',
                list: [
                    {
                        pagePath: '/pages/index/index',
                        iconPath: '/static/tarbar/home1.png',
                        selectedIconPath: '/static/tarbar/home2.png',
                        text: '首页'
                    },
                    {
                        pagePath: '/pages/foodie/foodie',
                        iconPath: '/static/tarbar/foodie1.png',
                        selectedIconPath: '/static/tarbar/foodie2.png',
                        text: '吃什么'
                    },
                    {
                        pagePath: '/pages/releaseBtn/releaseBtn',
                        iconPath: '',
                        selectedIconPath: '/static/tarbar/release.png'
                    },
                    {
                        pagePath: '/pages/discover/discover',
                        iconPath: '/static/tarbar/discover1.png',
                        selectedIconPath: '/static/tarbar/discover2.png',
                        text: '发现'
                    },
                    {
                        pagePath: '/pages/personal/personal',
                        iconPath: '/static/tarbar/personal1.png',
                        selectedIconPath: '/static/tarbar/personal2.png',
                        text: '我的'
                    }
                ]
            },
            oldSelected: 0, // 记录之前访问的索引; 值为2的时候显示遮罩
            isShowMask: false
        };
    },
    onLoad() {},
    methods: {
        setSelected(index) {
            console.log(index);
            if (index != 2) {
                uni.switchTab({
                    url: this.tabBar.list[index].pagePath
                });
            } else {
                this.isShowMask = true;
            }
            this.$forceUpdate();
        },
        closeMask() {
            this.isShowMask = false;
        }
    }
};
</script>

<style>
.tarbar {
     100%;
    z-index: 9999;
    position: fixed;
}
.tarbar-list {
     100%;
    height: 120upx;
    background: #4d586f;
    position: fixed;
    left: 0;
    bottom: 0;
}
.tarbar-list-ul {
     100%;
    height: 100%;
    padding: 20upx 50upx;
    display: flex;
    justify-content: space-between;
    box-sizing: border-box;
}
.tarbar-list-li {
     80upx;
    height: 80upx;
}
.tarbar-list-li-icon {
     50upx;
    height: 50upx;
    margin: 0 auto;
}
.tarbar-list-li-icon image {
     50upx;
    height: 50upx;
}
.tarbar-list-li-name {
     100%;
    text-align: center;
    line-height: 30upx;
    font-size: 20upx;
    height: 30upx;
}
.tarbar-list-li-center {
     100upx;
}
.tarbar-list-li-center .tarbar-list-li-icon,
.tarbar-list-li-center .tarbar-list-li-icon image {
     90upx;
    height: 60upx;
}
</style>
custom-tarbar.vue

release-popup.vue文件(弹窗)

uniapp 小程序实现自定义底部导航栏(tarbar)第4张uniapp 小程序实现自定义底部导航栏(tarbar)第5张
<template>
    <view class="mask">
        <view class="mask-top">
            <view class="mask-title">唯美食与爱不可辜负</view>
            <view class="mask-desc">对于美食的热爱,如果用一句话来形容,那就是:唯美食与爱不可辜负。</view>
            <view class="mask-btn">
                <view   v-for="(item, index) in btnList" :key="index" @click.stop="toRelease(index)">
                    <view   :class="item.icon"></view>
                    <view class="mask-btn-list-name">{{ item.name }}</view>
                </view>
            </view>
        </view>
        <view   @click.stop="closeMask"></view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            btnList: [
                { icon: 'iconfabu-fashipin', name: '发视频' },
                { icon: 'iconfabu-facaipu', name: '发菜谱' },
                { icon: 'iconfabu-shaimeishi', name: '晒美食' },
                { icon: 'iconfabu-caogaoxiang', name: '草稿箱' }
            ]
        };
    },
    methods: {
        closeMask() {
            this.$emit('close-mask');
        },
        toRelease(index) {
            let url = '';
            if (index == 1) {
                url = '';
            } else if (index == 2) {
                url = '/pages/release/release';
            } else if (index == 3) {
                url = '';
            } else {
                url = '';
            }
            if (!url) {
                this.getMessage('敬请期待', 0);
                return;
            }
            uni.navigateTo({
                url: url
            });
        },
        getMessage(title, icon = 0, duration = 2000, mask = true) {
            icon = icon == 1 ? 'success' : icon == 2 ? 'loading' : 'none';
            uni.showToast({
                title: title,
                icon: icon,
                duration: duration,
                mask: mask
            });
        }
    }
};
</script>

<style>
.mask {
    padding: 100upx 80upx 0;
     100%;
    height: 100vh;
    background: #f6f7f7;
    position: relative;
    box-sizing: border-box;
    /* left: 0;
    top: 0; */
}
.mask-top {
     100%;
    color: #595656;
}
.mask-title {
    height: 100upx;
    line-height: 100upx;
    font-size: 50upx;
}
.mask-desc {
     100%;
    height: 260upx;
    line-height: 50upx;
    font-size: 30upx;
}
.mask-btn {
     100%;
    height: 560upx;
    padding: 0 10upx;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-content: space-between;
    box-sizing: border-box;
}
.mask-btn-list {
     260upx;
    height: 260upx;
    border-radius: 20upx;
    background: #007aff;
    padding: 70upx;
    box-sizing: border-box;
}
.mask-btn-list:nth-child(1) {
    background: -webkit-linear-gradient(30deg, #fadde9, #eb68a3); /* Safari 5.1 - 6.0 */
    background: -o-linear-gradient(30deg, #fadde9, #eb68a3); /* Opera 11.1 - 12.0 */
    background: -moz-linear-gradient(30deg, #fadde9, #eb68a3); /* Firefox 3.6 - 15 */
    background: linear-gradient(30deg, #fadde9, #eb68a3); /* 标准的语法(必须放在最后) */
    color: #5d163a;
}
.mask-btn-list:nth-child(2) {
    background: -webkit-linear-gradient(30deg, #d7e378, #1eb7b6); /* Safari 5.1 - 6.0 */
    background: -o-linear-gradient(30deg, #d7e378, #1eb7b6); /* Opera 11.1 - 12.0 */
    background: -moz-linear-gradient(30deg, #d7e378, #1eb7b6); /* Firefox 3.6 - 15 */
    background: linear-gradient(30deg, #d7e378, #1eb7b6); /* 标准的语法(必须放在最后) */
    color: #113d41;
}
.mask-btn-list:nth-child(3) {
    background: -webkit-linear-gradient(30deg, #a7daf6, #7b4d9b); /* Safari 5.1 - 6.0 */
    background: -o-linear-gradient(30deg, #a7daf6, #7b4d9b); /* Opera 11.1 - 12.0 */
    background: -moz-linear-gradient(30deg, #a7daf6, #7b4d9b); /* Firefox 3.6 - 15 */
    background: linear-gradient(30deg, #a7daf6, #7b4d9b); /* 标准的语法(必须放在最后) */
    color: #2a274c;
}
.mask-btn-list:nth-child(4) {
    background: -webkit-linear-gradient(30deg, #ffef01, #ee7437); /* Safari 5.1 - 6.0 */
    background: -o-linear-gradient(30deg, #ffef01, #ee7437); /* Opera 11.1 - 12.0 */
    background: -moz-linear-gradient(30deg, #ffef01, #ee7437); /* Firefox 3.6 - 15 */
    background: linear-gradient(30deg, #ffef01, #ee7437); /* 标准的语法(必须放在最后) */
    color: #562f15;
}
.mask-btn-list-icon {
     100%;
    height: 70upx;
    line-height: 70upx;
    text-align: center;
    font-size: 50upx;
}
.mask-btn-list-name {
     100%;
    height: 50upx;
    line-height: 50upx;
    text-align: center;
    font-size: 30upx;
}
.mask-close {
     80upx;
    height: 80upx;
    position: absolute;
    left: 50%;
    margin-left: -40upx;
    bottom: 40upx;
    font-size: 50upx;
    line-height: 80upx;
    text-align: center;
    color: #595656;
}
</style>
release-popup.vue

2.调用

uniapp 小程序实现自定义底部导航栏(tarbar)第6张uniapp 小程序实现自定义底部导航栏(tarbar)第7张
<template>
    <view class="content">
        <custom-tarbar :selected="0"></custom-tarbar>
        <view class="home">
            home
        </view>
    </view>
</template>

<script>
import customTarbar from '@/wxcomponents/custom-tarbar/custom-tarbar.vue';
export default {
    components: {
        'custom-tarbar': customTarbar
    }
};
</script>

<style>
.content {
     100%;
}
</style>
View Code

注意:组件传值(selected)索引对应默认从0开始

免责声明:文章转载自《uniapp 小程序实现自定义底部导航栏(tarbar)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux-vim使用操作大全4种常用扒站工具(webzip、ha_TeleportPro、Offline Explorer、wget)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android 自定义View修炼-自定义加载进度动画XCLoadingImageView

一、概述 本自定义View,是加载进度动画的自定义View,继承于ImageView来实现,主要实现蒙层加载进度的加载进度效果。 支持水平左右加载和垂直上下加载四个方向,同时也支持自定义蒙层进度颜色。 直接看下面的效果图吧。 二、效果图 废话不说,先来看看效果图吧~~ 三、实现原理方案 1、自定义View-XCLoadingImageView,继承Ima...

CSS背景属性

1、 background-color     背景颜色 2、 background-image    背景图片 3、 Background-repeat    repeat(默认)  |  no-repeat |   repeat-x   |  repeat-y     背景平铺 4 、Background-position  left  |  r...

CSS躬行记(8)——裁剪和遮罩

一、 裁剪 裁剪(clipping)能让元素显示指定形状的区域,在布局时可起点缀的作用,丰富了视觉呈现。注意,裁剪本质上只是让元素的部分区域透明,由此可知,裁剪完后元素所占的空间仍旧会保留。裁剪最早是在CSS 2.1时代由clip属性引入,但该属性只能应用于绝对定位的元素,并且只能裁剪成矩形。CSS3提供了强大的clip-path属性,突破了clip属性的...

Android 自定义View使用示例(三)

转载 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gamil.com 前言:   通过Android 自定义View和Android 自定义View使用示例(二),我们知道了如何使用自定义的View,以及Android绘制V...

XML做数据库操作之 我浑了

只前我看邵志东老师的教程做了一个实例 地址是 http://thcjp.cnblogs.com/archive/2006/05/06/392739.html但是删除的问题始终没有解决,这次做留言本 http://thcjp.cnblogs.com/archive/2006/06/26/435962.html我实在想不到好办法来维护广告控件使用的那个XML文...

CSS3(七) 前端预处理技术(Less、Sass、CoffeeScript)

目录 一、Less 1.1、概要 1.2、变量 1.3、解析Less 1.3.1、在线处理 1.3.2、预处理 1.4、混入(Mixins) 1.5、嵌套    1.6、运算 1.7、函数 1.8、继承     1.9、作用域 1.10、注释 二、Sass 2.1、变量 2.2、嵌套 2.3、导入 2.4、mixin 混入 2.5、扩展/继承...