Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
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小時內與您取得聯系。