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实现商城底部导航栏
在商城第一篇的开篇当中,我们看到商城的效果图里面有一个底部导航栏效果,如下图所示: 今天我们就来实现商城底部导航栏,最终效果图如下所示: 那么这种效果是如何实现,实现的方式有很多种,最常见的就是使 ...
随机推荐
- Jmeter常见报错信息: ERROR - jmeter.protocol.http.proxy.ProxyControl: Could not initialise key store java.io.IOException: Cannot run program "keytool"
JMeter 2.10 用的新方法来录制HTTPS请求Java 7. 录制的过程中会碰到一些问题或者报错,就目前碰到的,做出一些总结. ERROR - jmeter.protocol.http.pro ...
- C语言:数组数据交换
//交换数组中各个变量的值:第1和最后一个交换,第2与倒数第二个交换 #include <stdio.h> int main() { int a[]={1,2,3,4,5,6,7,8,9} ...
- .netcore第三方登录授权:10分钟急速接入
前言 很多对外应用的开发都考虑接入第三方登录来提高用户的体验感,避免用户进行繁琐的注册登录(登录后的完善资料必不可免). 而QQ.微信.支付宝.淘宝.微博等应用就是首选目标(无他,用户群体大,支持发开 ...
- 初探SpringRetry机制
重试是在网络通讯中非常重要的概念,尤其是在微服务体系内重试显得格外重要.常见的场景是当遇到网络抖动造成的请求失败时,可以按照业务的补偿需求来制定重试策略.Spring框架提供了SpringRetry能 ...
- Python基础之动态添加属性,方法,动态类,静态类
## 动态添加属性class Person: def __init__(self,name): self.name = name# 1.通过对象.属性名称来操作p = Person('KTModel' ...
- jmeter测试流程整理
背景 整理jmeter脚本编写流程,注意事项,常用组件,常见问题. 参看链接:https://www.cnblogs.com/pwj2lgx/p/10282422.html 参看:processOn思 ...
- 前端基础css(三)
HTML:用于显示页面的内容 CSS:用于以什么样的形式(样式)去显示 一. 选择器 [1] 标签/元素选择器 (整个页面的所有的相同的标签都显示统一的样式) h1{ font-size: 20px; ...
- PGSQL存储过程学习
一.存储过程定义: 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,它存储在数据库中,一次编译后永久有效,用户通过指定存储过程的名字并给出参 ...
- Spring Boot中使用时序数据库InfluxDB
除了最常用的关系数据库和缓存之外,之前我们已经介绍了在Spring Boot中如何配置和使用MongoDB.LDAP这些存储的案例.接下来,我们继续介绍另一种特殊的数据库:时序数据库InfluxDB在 ...
- Mongodb集成LDAP授权
一.环境简介 Mongodb enterprise v4.0.16 OpenLDAP v2.4.44 二.Mongodb集成LDAP的授权过程 客户端指定某种外部验证方式链接Mongodb: Mong ...