网站首页 > 基础教程 正文
效果展示
选中样式制作
- 将每个 tab 设置固定宽度。
- 每个tab 添加相对定位,居中,行高
- 添加伪类,伪类设置绝对定位,在底部。
- 设置伪类的宽度为 0%(伪类会继承该元素的宽度)
- 设置选中时候的伪类宽度为50%(视情况而定)
- 给元素添加过渡样式
HTML代码(wxml)
{{item.name}}
CSS(less):
.nav-bar{ position: relative; z-index: 10; height: 90upx; white-space: nowrap; background-color: #fbfbfb; .nav-item{ display: inline-block; width: 150upx; height: 90upx; text-align: center; line-height: 90upx; font-size: 30upx; color: #a4a4a4; position: relative; } .current{ color: #3f3f3f; font-weight: bold; } }
实现效果大致为这样的:
拓展
PS: 以上为纯CSS实现部分,如果项目 tab数量 为通过接口动态获取的,可以适当加入一些 js 计算。
JS 思路:
- 获取当前选中的 tab 的宽度
- 获取当前选中 tab 以及它之前全部 tab 的宽度总和。
- 获取当前屏幕宽度
- 判断当前选中 tab 是否超过屏幕中心点(当前选中 tab 以及它之前全部 tab 的宽度总和 - 当前选中 tab 宽度/2)
- 移动当前 tabs 到屏幕的重心点位置
大致为(以微信小程序为例):
let width = 0; // 当前选中选项卡及它之前的选项卡之和总宽度 let nowWidth = 0; // 当前选项卡的宽度 //获取可滑动总宽度 for (let i = 0; i <= index; i++) { let result = await this.getElSize('tab' + i); width += result.width; if(i === index){ nowWidth = result.width; } } // console.log(width, nowWidth, windowWidth) //等待swiper动画结束再修改tabbar this.$nextTick(() => { if (width - nowWidth/2 > windowWidth / 2) { //如果当前项越过中心点,将其放在屏幕中心 this.scrollLeft = width - nowWidth/2 - windowWidth / 2; console.log(this.scrollLeft) }else{ this.scrollLeft = 0; } if(typeof e === 'object'){ this.tabCurrentIndex = index; } this.tabCurrentIndex = index; })
ps: getElSize() 函数代码为:
getElSize(id) { return new Promise((res, rej) => { let el = uni.createSelectorQuery().select('#' + id); el.fields({ size: true, scrollOffset: true, rect: true }, (data) => { res(data); }).exec(); }); },
这样就可以实现动态 tab 切换了:
猜你喜欢
- 2024-11-09 值得一看!这些CSS小技巧帮助你提升80%的效率
- 2024-11-09 (收藏)你不知道的CSS技巧 css使用教程
- 2024-11-09 那些常用却又常忘记的css样式 那些常用却又常忘记的css样式是什么
- 2024-11-09 html页面常用css简单整理 html+css总结
- 2024-11-09 Vue项目接入Eslint与+Prettier+Husky自动格式化代码
- 2024-11-09 CSS性能优化你知多少 css3优化
- 2024-11-09 Web开发学习笔记(19)——CSS(6)常用样式
- 2024-11-09 如何用一行 CSS 实现 10 种现代布局?
- 2024-11-09 手把手搭建Vue3中后台框架-初始化项目
- 2024-11-09 以CSS+JavaScript实现水平无限滚动动画
- 最近发表
- 标签列表
-
- jsp (69)
- pythonlist (60)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- deletesql (62)
- c++模板 (62)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- console.table (62)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)