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 思路:
大致为(以微信小程序为例):
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 切换了:
天和大家分享如何修改radio样式
自定义颜色
自定义图片
全部代码直接可以运行,兼容性ie,火狐,谷歌
/*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/ ::-webkit-scrollbar { width: 4px; height: 4px; background-color: #F5F5F5; }
/*定义滚动条轨道 内阴影+圆角*/ ::-webkit-scrollbar-track { -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); border-radius: 10px; background-color: #FFF; }
/*定义滑块 内阴影+圆角*/ ::-webkit-scrollbar-thumb { border-radius: 10px; -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3); background-color: #AAA; }
*请认真填写需求信息,我们会在24小时内与您取得联系。