我们用Vue写手机端的项目,经常会写底部导航栏,我这里总结一套比较方便实用的底部导航栏方法,并且可以解决浏览器刷新选中状态消失的问题。也可以选择自适应屏幕。看一下效果,底部的图标是我自己找的,选中和未选中样式的图片,根据公司要求,你也可能会用fontsize去写。(全部代码黏贴到本文的最后面了)

关于布局的已经写过了,在这个博客文件中.
当然还有新的方法去写导航
首先在导航页面中的data()里边定义一个选中对应的变量isSelect,和循环遍历的数组,数组下面放图标对应的文字,和选中,未选中的图片地址。 注意:图片的地址不要直接写,直接写就是字符串,不仅会出现显示不出图片的情况,而且打包之后,还是这里地址,不会变。使用webpack提供的require引入图片地址就可以解决以上问题。

Nav.vue 文件

   data () {return {isSelect: '首页', // 默认首选页面是首页nav: [{title: '首页', url: require('../../assets/common/首页@2x.png'), url_one: require('../../assets/common/首页_active@2x.png')},{title: '分类', url: require('../../assets/common/分类@2x.png'), url_one: require('../../assets/common/分类_active@2x.png')},{title: '米圈', url: require('../../assets/common/米圈@2x.png'), url_one: require('../../assets/common/米圈_active@2x.png')},{title: '购物车', url: require('../../assets/common/购物车@2x.png'), url_one: require('../../assets/common/购物车_active@2x.png')},{title: '我的', url: require('../../assets/common/我的@2x.png'), url_one: require('../../assets/common/我的_active@2x.png')}]}},

设置了基本的数据
那我们在template 中

<template><div class="Nav"><ul><li v-for="item in nav" @click="selectNav(item.title)"><img :src="isSelect === item.title ? item.url_one : item.url" alt="item.title"><p :class="isSelect === item.title ? 'active' : ''">{{item.title}}</p></li></ul>  </div>
</template>

那么我们在script 中写入

  methods: {selectNav (title) { // 选中的是哪个路由导航this.isSelect = title}这个方法里还可以根据title的值去跳转到相应的路由,这样一个基本的底部导航栏就是实现了。或者methods: {selectNav (title) {this.isSelect = titleswitch (title) {case '首页': this.$router.push('/home')breakcase '分类': this.$router.push('/classify')breakcase '米圈': this.$router.push('/ring')breakcase '购物车': this.$router.push('/shop')breakcase '我的': this.$router.push('/mine')break}sessionStorage.setItem('isSelect', this.isSelect) // 存入sessionStorage里面}}但是 电脑调试的时候会发现,刷新浏览器后,选中的状态还是有可能会消失。

解决办法:
每次点击切换底部导航的时候,把选中的状态存入sessStorage里边。在mounted钩子里把这个状态在取出来赋值给这个isSelect变量就可以实现选中状态不消失了。

mounted () {this.isSelect = sessionStorage.getItem('isSelect')
},
methods: {selectNav (title) {this.isSelect = titlesessionStorage.setItem('isSelect', this.isSelect)}
}

但是经过测试,新的问题又发现了,比如当前在“米圈”这个状态上,我在浏览器上直接输入“http://localhost:8080/#/ring”,这样用上面的办法就解决不了问题了。最好的办法就是和路由绑定无论点击,还是浏览器上输入路由改变,都正确显示选中状态。
就是在router/index.js里边映射组件路由时,加上对应的name ,最好用英文

  routes: [{path: '/',redirect: '/home'},{path: '/home',name: '首页',component:() => import('../views/Home.vue')},{path: '/classify',name: '分类',component:() => import('../views/Classify.vue')},{path: '/ring',name: '米圈',component: () => import('../views/Ring.vue')},{path: '/shop',name: '购物车',component: () => import('../views/Shop.vue')},{path: '/mine',name: '我的', component: () => import('../views/Mine.vue')}]

这时候nav.vue文件中mounted钩子和methods方法里边的代码改为:

mounted () {this.isSelect = this.$route.name
},
methods: {selectNav (title) {this.isSelect = this.$route.nameswitch (title) {case '首页': this.$router.push('/home')breakcase '分类': this.$router.push('/classify')breakcase '米圈': this.$router.push('/ring')breakcase '购物车': this.$router.push('/shop')breakcase '我的': this.$router.push('/mine')break}}
}

注意的是
手机端一般要求自适应各种大小的手机端屏幕,你可以选择用媒体查询,或者js控制font-size。这里我用的是js控制font-size,在index.html引入下面的js。

 * rem计算方式:设计图尺寸px / 100 = 实际rem  【例: 100px = 1rem,32px = 0.32rem】 */
!function (window) {/* 设计图文档宽度 */var docWidth = 750;var doc = window.document,docEl = doc.documentElement,resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';var recalc = (function refreshRem () {var clientWidth = docEl.getBoundingClientRect().width;/* 8.55:小于320px不再缩小,11.2:大于420px不再放大 */docEl.style.fontSize = Math.max(Math.min(20 * (clientWidth / docWidth), 11.2), 8.55) * 5 + 'px';return refreshRem;})();/* 添加倍屏标识,安卓为1 */docEl.setAttribute('data-dpr', window.navigator.appVersion.match(/iphone/gi) ? window.devicePixelRatio : 1);if (/iP(hone|od|ad)/.test(window.navigator.userAgent)) {/* 添加IOS标识 */doc.documentElement.classList.add('ios');/* IOS8以上给html添加hairline样式,以便特殊处理 */if (parseInt(window.navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/)[1], 10) >= 8)doc.documentElement.classList.add('hairline');}if (!doc.addEventListener) return;window.addEventListener(resizeEvt, recalc, false);doc.addEventListener('DOMContentLoaded', recalc, false);
}(window);// 特别注意:是不需要再除以2的!无论设计图什么尺寸,算法一致。但需修改js 中 docWidth 变量为设计图宽度;默认设计图文档宽度为750px; 一些不使用rem的CSS属性。包括但不限于:border-width、border-radius、box-shadow、transform、background-size;

完整的代码下边就是

<template><div class="common_foot"><ul><li v-for="item in nav" @click="selectNav(item.title)"><img :src="isSelect === item.title ? item.url_one : item.url" alt="item.title"><p :class="isSelect === item.title ? 'active' : ''">{{item.title}}</p></li></ul></div>
</template><script>export default {data () {return {isSelect: '首页',nav: [{title: '首页', url: require('../../assets/common/首页@2x.png'), url_one: require('../../assets/common/首页_active@2x.png')},{title: '分类', url: require('../../assets/common/分类@2x.png'), url_one: require('../../assets/common/分类_active@2x.png')},{title: '米圈', url: require('../../assets/common/米圈@2x.png'), url_one: require('../../assets/common/米圈_active@2x.png')},{title: '购物车', url: require('../../assets/common/购物车@2x.png'), url_one: require('../../assets/common/购物车_active@2x.png')},{title: '我的', url: require('../../assets/common/我的@2x.png'), url_one: require('../../assets/common/我的_active@2x.png')}]}},mounted () {this.isSelect = this.$route.name // 重要的在这里和下边,也可以直接用判断 window.location.hash.indexOf('name这里是router里的name' > -1) 然后重新赋值默认选项// 用了这个那就不用methods里的方法了,其实都一样// 默认是首页了/**if(window.location.hash.indexOf('fenlei')>-1){ // this.selectIndex =1 // 分类} else if (window.location.hash.indexOf('miquan')>-1){this.selectIndex =2 // 米圈} else if (window.location.hash.indexOf('gouwuche')>-1){this.selectIndex =3 // 购物车} else if (window.location.hash.indexOf('wode')>-1){this.selectIndex =4 // 我的} else {this.selectIndex = 0 // 首页}*/},methods: {selectNav (title) {this.isSelect = this.$route.nameswitch (title) {case '首页': this.$router.push('/home')breakcase '分类': this.$router.push('/classify')breakcase '米圈': this.$router.push('/ring')breakcase '购物车': this.$router.push('/shop')breakcase '我的': this.$router.push('/mine')break}}}}
</script><style lang="less" scoped>.common_foot>ul{position: fixed;bottom: 0;z-index: 1000;height: 0.98rem;width: 100%;overflow: hidden;background-color: white;li{float: left;width: 25%;height: 100%;text-align: center;cursor: pointer;padding: 0.15rem 0 0.13rem 0;}p{font-size: 0.2rem;color: #7f7f7f;}img{width: 0.48rem;height: 0.45rem;}.active{color: #ffd100;}}
</style>

解决了。
当然了还有一个办法,监听

  methods: {// 页面切换tapPage(index, routeName) {this.selectIndex = index; // 下标// 编程式路由跳转//  是因为vue-router 3.0之后的版本中,push跳转使用了异步的处理//    当插入相同的地址信息之后就会报错,可以添加catch对异常做捕获this.$router.push({name: routeName, // 传递路由的name}).catch(() => {// catch对异常做捕获});},},// 监听 路由比对,设定index 下标watch: {$route() {if (this.$route.path == "/home") {this.selectIndex = 0;}if (this.$route.path == "/list") {this.selectIndex = 1;}if (this.$route.path == "/cart") {this.selectIndex = 2;}if (this.$route.path == "/user") {this.selectIndex = 3;}},},// 钩子函数这里也要注意下mounted() { // 浏览器路由// console.log(window.location);// 根据当前的地址信息, 设置选中的图标索引.解决刷新之后的问题//  没有使用this.$route.xx 进行设置,因为有些时候获取不到路由信息if (window.location.hash.indexOf("home") > -1) {this.selectIndex = 0;}if (window.location.hash.indexOf("list") > -1) {this.selectIndex = 1;}if (window.location.hash.indexOf("cart") > -1) {this.selectIndex = 2;}if (window.location.hash.indexOf("user") > -1) {this.selectIndex = 3;}},

vue底部导航栏(选中状态刷新不消失)解决相关推荐

  1. vue底部跳转_详解Vue底部导航栏组件

    不多说直接上代码 BottomNav.vue: {{item.name}} export default{ props:['idx'], data(){ return { items:[{ cls:& ...

  2. 状态输出导航栏html,Vue实现导航栏效果(选中状态刷新不消失)_百厌_前端开发者...

    用 1.首先把这些小图片放到src/assets路径下面(自动base64编码) 2.在data()里边定义一个选中对应的变量isSelect,和循环遍历的数组,数组下面放图标对应的文字,和选中,未选 ...

  3. 开发vue底部导航栏组件

    这个想法源于最近自己在开发一个移动端博客的一个底部导航栏,原型设计如下: 我们来分析一下这个导航栏,其实很简单啦,就是自适应固定在底部 我们可以使用CSS3属性display:flex设置父级盒子为伸 ...

  4. 微信小程序自定义底部导航栏遮挡页面内容(已解决)

    今天也是努力写毕设的一天~ 这几天在实现旅行日记的笔记详情界面,先实现了自定义的底部导航栏,在这里我使用的是iView Weapp,具体的介绍我放在这里了~ 快速上手 iView Weapp 跟着里面 ...

  5. android底部导航栏选中动画,Android选中突出背景效果的底部导航栏功能

    今天在群里看到一个底部导航选中突出效果像这样 就想着 这个应该怎么做呢,我记得类似咸鱼那种的是中间突出,不像这种 是选中哪个,哪个就突出 第一种方法 简单快捷,让UI帮忙切几张带突出背景的图片, 选中 ...

  6. html 导航栏 选中状态,jQuery“导航栏点击选中效果“代码

    现在的网站菜单导航栏几乎都有点击选中效果,正好今天我在开发项目中也需要用上这样的效果,下面就给大家分享一下,亲测有效,大家放心使用. 1.引入jQuery核心文件 2.加入"jQuery导航 ...

  7. html 导航栏 选中状态,CSS导航菜单高亮选中菜单项

    我开发了一个CSS导航菜单,并为每个导航项我写了php.file.CSS导航菜单高亮选中菜单项 homepage.php My First Try Of CSS Navigation Bar /* B ...

  8. 【Flutter】StatefulWidget 组件 ( 底部导航栏组件 | BottomNavigationBar 组件 | BottomNavigationBarItem 组件 | 选项卡切换 )

    文章目录 一.BottomNavigationBar 组件 二.BottomNavigationBarItem 组件 三.BottomNavigationBar 底部导航栏代码示例 四.BottomN ...

  9. mui实现底部导航栏页面切换

    1 问题描述 mui是一款最接近原生的框架,对于会使用的人来说十分的便捷易操作,但对于像笔者这样刚入门的小白可是十分的陌生和走了太多的弯路. 对于那些不懂的前端的人,或者是一些刚入门html的人,他们 ...

最新文章

  1. 设置/修改centos上的swap交换分区的方法
  2. 用户自定义的Javascript函数名字与内置函数或宿主函数名字相同时产生的问题。...
  3. 使用 _tprintf 宏兼容多字节字符集和Unicode字符集
  4. GitHub上如何进行PR(Pull Request)操作——简版
  5. jQuery_事件学习
  6. Linux可以对目录进行硬链接,Linux硬链接与软链接原理及用法解析
  7. api商品分享源码_谈谈微服务中的 API 网关(API Gateway)
  8. ZOJ1222 Just the Facts【大数+模除】
  9. iOS: AFNetworking手动配置(iOS7.1, AF2.2.4)
  10. 64位 centos 服务器 编译安装 gcc-4.6.2 和 codeviz
  11. quartz 定时任务 表达式
  12. 各大物联网通信技术对比
  13. 模拟京东登陆 java_java京东自动登录
  14. Axure 软件自带的图标元件Icons无法显示的问题解决方法
  15. Efficient Heterogeneous Collaborative Filtering without Negative Sampling for Recommendation (2020)
  16. 【Java、Redis】通过中心经纬度与半径获取范围内的结果集(类似附近的人)
  17. 百度云安装mysql_安装mysql5.7和mysql8.0(可同时安装在windows中)附百度云下载链接
  18. 局域网与城域网 - 以太网标准(百兆 100Base、千兆 1000Base、万兆 10GBase)
  19. c++数独游戏3.2
  20. # 设置防火墙白名单

热门文章

  1. 上市条件 - 打造企业上市系列文章
  2. elementui 表格表头竖着显示_ElementUI中table表格自定义表头Tooltip文字提示
  3. [bzoj3609]人人尽说江南好
  4. android停按钮在哪,哔哩哔哩投屏按钮在哪里 哔哩哔哩投屏安卓手机操作流程
  5. oracle数据库内存不足导致查询变慢,经验:探索内存问题如何造成数据库性能严重异常...
  6. 玩具具体可以分为哪些种类
  7. 【Linux云计算架构:第三阶段-Linux高级运维架构】第19章——安装Kali黑客操作系统-利用ettercap实施中间人攻击
  8. Extracted SQL state class '99' from value '99999'
  9. 使用git bash的git push origin main或git push origin master提交错误
  10. 哔哩哔哩(上海)开发实习生(Java方向)面试复盘和答案总结