Vue 如何实现一个底部导航栏组件
参考网址: https://www.jianshu.com/p/088936b7b1bd/
Vue 如何实现一个底部导航栏组件
可以看到父组件是知道我点击了底部TabBar的哪个item的。
实现
实现template 和style
我用的布局工具是bootstrap,图标是阿里巴巴的iconfont
<template>
<div id="TabBar" class="tab-bar row">
<div class="col-xs-4 tab-bar-item">
<div class="row">
<i class="iconfont icon-daohangshouye"></i>
</div>
<div class="row">
<span>首页</span>
</div>
</div>
<div class="col-xs-4 tab-bar-item">
<div class="row">
<i class="iconfont icon-shangjia"></i>
</div>
<div class="row">
<span>商户</span>
</div>
</div>
<div class="col-xs-4 tab-bar-item">
<div class="row">
<i class="iconfont icon-huiyuan"></i>
</div>
<div class="row">
<span>我的</span>
</div>
</div>
</div>
</template>
<style scoped>
.tab-bar {
background-color: white;
height: 54px;
border: 0 solid rgb(210, 210, 210);
border-top-width: 1px;
position: fixed;
margin: auto;
bottom: 0;
width: 100%;
}
.tab-bar-item {
height: 54px;
}
i {
font-size: 25px;
}
</style>
实现点击事件
在每个tab-bar-item的div上添加@click绑定点击事件并且传递当前是哪个item,第一个item就可以传一个tag=0的参数。实现如下:
<div class="col-xs-4 tab-bar-item" @click="didClickedItem(0)">
<div class="row">
<i class="iconfont icon-daohangshouye"></i>
</div>
<div class="row">
<span>首页</span>
</div>
</div>
didClickedItem: function (tag) {
if (tag === 0) {
} else if (tag === 1) {
} else if (tag === 2) {
}
}
点击完肯定需要让那个item进入选中的状态,也就是变亮,可以通过v-bind:class去做。data存一个数组放bool变量,因为第一个item肯定默认点亮,所以数组第一个元素为true。每次点击别的item时先把数组元素全部置位false,然后把选中的item所对应的数组元素改为true就可以实现选中效果。实现如下:
data: function () {
return {
actives: [true, false, false]
}
}
<div class="col-xs-4 tab-bar-item" @click="didClickedItem(0)" v-bind:class="{active: actives[0]}">
<div class="row">
<i class="iconfont icon-daohangshouye"></i>
</div>
<div class="row">
<span>首页</span>
</div>
</div>
didClickedItem: function (tag) {
this.actives = this.actives.map(function () {
return false;
});
this.actives[tag] = true;
}
.active {
color: #1E90FF;
}
对外暴露方法
选中一个item肯定需要让父组件知道你选了什么,所以肯定要对外暴露方法,这个方法在didClickedItem这个方法最后一行加一行代码即可
# TabBar组件里
this.$emit('select-item', tag);
# 父组件使用方法
<template>
<div id="app">
<div style="font-size: 20px">{{item}}</div>
<TabBar @select-item="onClickTabBarItem"/>
</div>
</template>
<script>
import TabBar from './components/TabBar'
export default {
name: 'app',
data: function () {
return {
item: 0
}
},
components: {
TabBar
},
methods: {
onClickTabBarItem: function (tag) {
this.item = tag;
}
}
}
</script>
完整代码
<template>
<div id="TabBar" class="tab-bar row">
<div class="col-xs-4 tab-bar-item" @click="didClickedItem(0)" v-bind:class="{active: actives[0]}">
<div class="row">
<i class="iconfont icon-daohangshouye"></i>
</div>
<div class="row">
<span>首页</span>
</div>
</div>
<div class="col-xs-4 tab-bar-item" @click="didClickedItem(1)" v-bind:class="{active: actives[1]}">
<div class="row">
<i class="iconfont icon-shangjia"></i>
</div>
<div class="row">
<span>商户</span>
</div>
</div>
<div class="col-xs-4 tab-bar-item" @click="didClickedItem(2)" v-bind:class="{active: actives[2]}">
<div class="row">
<i class="iconfont icon-huiyuan"></i>
</div>
<div class="row">
<span>我的</span>
</div>
</div>
</div>
</template>
<script>
export default {
name: "TabBar",
props: {
},
data: function () {
return {
actives: [true, false, false]
}
},
methods: {
didClickedItem: function (tag) {
if (tag === 0) {
this.actives = this.actives.map(function () {
return false;
});
this.actives[0] = true;
} else if (tag === 1) {
this.actives = this.actives.map(function () {
return false;
});
this.actives[1] = true;
} else if (tag === 2) {
this.actives = this.actives.map(function () {
return false;
});
this.actives[2] = true;
}
this.$emit('select-item', tag);
}
}
}
</script>
<style scoped>
.tab-bar {
background-color: white;
height: 54px;
border: 0 solid rgb(210, 210, 210);
border-top-width: 1px;
position: fixed;
margin: auto;
bottom: 0;
width: 100%;
}
.tab-bar-item {
height: 54px;
}
.active {
color: #1E90FF;
}
i {
font-size: 25px;
}
</style>
Vue 如何实现一个底部导航栏组件的更多相关文章
- 微信小程序自定义底部导航栏组件+跳转
微信小程序本来封装有底部导航栏,但对于想自定义样式和方法的开发者来说,这并不是很好. 参考链接:https://github.com/ljybill/miniprogram-utils/tree/ma ...
- Flutter——BottomNavigationBar组件(底部导航栏组件)
BottomNavigationBar常用的属性: 属性名 说明 items List<BottomNavigationBarItem> 底部导航条按钮集合 iconSize icon c ...
- TextView+Fragment实现底部导航栏
前言:项目第二版刚上线没多久,产品又对需求进行了大改动,以前用的是左滑菜单,现在又要换成底部导航栏,于是今天又苦逼加班了.花了几个小时实现了一个底部导航栏的demo,然后总结一下.写一篇博客.供自己以 ...
- 二、Fragment+RadioButton实现底部导航栏
在App中经常看到这样的tab底部导航栏 那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activi ...
- Android底部导航栏
Android底部导航栏 今天简单写了一个底部导航栏,封装了一个库,用法比较简单 效果图 Github地址:https://github.com/kongqw/KqwBottomNavigation ...
- Android开源项目——带图标文字的底部导航栏IconTabPageIndicator
接下来的博客计划是,在<Android官方技术文档翻译>之间会发一些Android开源项目的介绍,直接剩下的几篇Android技术文档发完,然后就是Android开源项目和Gradle翻译 ...
- TabHost实现底部导航栏
源代码及可执行文件下载地址:http://files.cnblogs.com/rainboy2010/tabnavigation.zip 现在很多Android应用界面都采用底部导航 ...
- Flutter - 创建底部导航栏
之前写过的一篇文章介绍了 Flutter - 创建横跨所有页面的侧滑菜单, 这次就一起来学习一下底部导航栏. 底部导航栏在ios平台上非常常见,app store就是这样的风格.还有就是大家最常用的微 ...
- Android商城开发系列(三)——使用Fragment+RadioButton实现商城底部导航栏
在商城第一篇的开篇当中,我们看到商城的效果图里面有一个底部导航栏效果,如下图所示: 今天我们就来实现商城底部导航栏,最终效果图如下所示: 那么这种效果是如何实现,实现的方式有很多种,最常见的就是使 ...
随机推荐
- 没事就要多做多练,Shell脚本循环例题做一做
Shell脚本循环例题 一.示例1 二.示例2 三.示例3 四.示例4 ...
- 如何在Apache HttpClient中设置TLS版本
1.简介 Apache HttpClient是一个底层.轻量级的客户端HTTP库,用于与HTTP服务器进行通信. 在本教程中,我们将学习如何在使用HttpClient时配置支持的传输层安全(TLS)版 ...
- 建立第一个SCRAPY的具体过程
1.安装SCRAPY2.进入CMD:执行:SCRAPY显示: Scrapy 1.8.0 - no active project Usage: scrapy <command> [optio ...
- python 交换变量的值 不需要借助第三个变量
>>> a,b,c,d=1,2,3,4>>> a,b,c,d=d,c,b,a>>> print(a,b,c,d)4 3 2 1>>&g ...
- ffmpeg入门篇-滤镜的基本使用
转发自白狼栈:查看原文 滤镜 什么是滤镜?百度百科介绍说"滤镜主要是用来实现图像的各种特殊效果......". 我们最早在ffmpeg是如何转码的一文中了解过滤镜,来回顾下当时的转 ...
- ACM金牌选手讲解LeetCode算法《栈和队列的高级应用》
大家好,我是编程熊,双非逆袭选手,字节跳动.旷视科技前员工,ACM金牌,保研985,<ACM金牌选手讲解LeetCode算法系列>作者. 上一篇文章讲解了<线性表>中的数组.链 ...
- CSAPP:datalab实验记录
CSAPP:datalab实验记录 bitXor /* * bitXor - x^y using only ~ and & * Example: bitXor(4, 5) = 1 * Lega ...
- ZYNQ FLASH+EMMC手动移植LINUX启动
前言 虽可使用Petalinux进行移植,简单方便,但为了更清楚明白的了解整个流程,还是尝试了一波手动移植. 参考资料 ZYNQ Linux 移植:包含petalinux移植和手动移植debian9 ...
- 如何在 PyCharm 中设置 Python 代码模板
#!/usr/bin/env python # -*- coding: utf-8 -*- # Created by iFantastic on $DATE if __name__ == '__mai ...
- npm 安装、卸载模块
npm安装模块 [npm install xxx]利用 npm 安装xxx模块到当前命令行所在目录:[npm install -g xxx]利用npm安装全局模块xxx:本地安装时将模块写入packa ...