微信小程序?qū)崿F(xiàn)滾動(dòng)Tab選項(xiàng)卡
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)滾動(dòng)Tab選項(xiàng)卡的具體代碼,供大家參考,具體內(nèi)容如下

最終效果如上。問(wèn)題:
1、tab標(biāo)題總共8個(gè),所以一屏無(wú)法全部顯示。
2、tab內(nèi)容區(qū)左右滑動(dòng)切換時(shí),tab標(biāo)題隨即做標(biāo)記(active)。
3、當(dāng)active的標(biāo)題不在當(dāng)前屏顯示時(shí),要使其能顯示到當(dāng)前屏中。
一、wxml結(jié)構(gòu)
tab標(biāo)題因一排八個(gè),所以使用 scroll-view組件,使其可橫向滾動(dòng)。
tab內(nèi)容可左右滑動(dòng)切換,使用swiper組件實(shí)現(xiàn)
為了偷懶,所以數(shù)據(jù)都通過(guò)wx:for遍歷重復(fù)出來(lái)。
說(shuō)明:
1、設(shè)置data-current屬性用于:點(diǎn)擊當(dāng)前項(xiàng)時(shí),通過(guò)點(diǎn)擊事件swichNav中處理e.dataset.current取到點(diǎn)擊的目標(biāo)值。
2、swiper組件的current組件用于控制當(dāng)前顯示哪一頁(yè)
3、swiper組件綁定change事件switchTab,通過(guò)e.detail.current拿到當(dāng)前頁(yè)
<view >
<scroll-view scroll-x="true" class="tab-h" scroll-left="{{scrollLeft}}">
<view class="tab-item {{currentTab==0?'active':''}}" data-current="0" bindtap="swichNav">健康</view>
<view class="tab-item {{currentTab==1?'active':''}}" data-current="1" bindtap="swichNav">情感</view>
<view class="tab-item {{currentTab==2?'active':''}}" data-current="2" bindtap="swichNav">職場(chǎng)</view>
<view class="tab-item {{currentTab==3?'active':''}}" data-current="3" bindtap="swichNav">育兒</view>
<view class="tab-item {{currentTab==4?'active':''}}" data-current="4" bindtap="swichNav">糾紛</view>
<view class="tab-item {{currentTab==5?'active':''}}" data-current="5" bindtap="swichNav">青蔥</view>
<view class="tab-item {{currentTab==6?'active':''}}" data-current="6" bindtap="swichNav">全部</view>
<view class="tab-item {{currentTab==7?'active':''}}" data-current="7" bindtap="swichNav">其他</view>
</scroll-view>
<swiper class="tab-content" current="{{currentTab}}" duration="300" bindchange="switchTab"
style="height:{{winHeight}}rpx">
<swiper-item wx:for="{{[0,1,2,3,4,5,6,7]}}">
<scroll-view scroll-y="true" class="scoll-h" >
<block wx:for="{{[1,2,3,4,5,6,7,8]}}" wx:key="*this">
<view class="item-ans">
<view class="avatar">
<image class="img" src="/avatar.png"></image>
</view>
<view class="expertInfo">
<view class="name">歡顏</view>
<view class="tag">知名情感博主</view>
<view class="answerHistory">134個(gè)回答,2234人聽(tīng)過(guò) </view>
</view>
<navigator url="/pages/askExpert/expertDetail" class="askBtn">問(wèn)TA</navigator>
</view>
</block>
</scroll-view>
</swiper-item>
</swiper>
</view>
二、js部分
微信小程序在開(kāi)發(fā)起來(lái),個(gè)人感覺(jué)挺像vue的,以數(shù)據(jù)驅(qū)動(dòng)視圖的更新。所以在小程序中,不能直接操作dom,當(dāng)然也不能使用jquery之類的庫(kù)。
var app = getApp();
Page({
data:{
winHeight:"",//窗口高度
currentTab:0, //預(yù)設(shè)當(dāng)前項(xiàng)的值
scrollLeft:0, //tab標(biāo)題的滾動(dòng)條位置
expertList:[{ //假數(shù)據(jù)
img:"avatar.png",
name:"歡顔",
tag:"知名情感博主",
answer:134,
listen:2234
}]
},
// 滾動(dòng)切換標(biāo)簽樣式
switchTab:function(e){
this.setData({
currentTab:e.detail.current
});
this.checkCor();
},
// 點(diǎn)擊標(biāo)題切換當(dāng)前頁(yè)時(shí)改變樣式
swichNav:function(e){
var cur=e.target.dataset.current;
if(this.data.currentTaB==cur){return false;}
else{
this.setData({
currentTab:cur
})
}
},
//判斷當(dāng)前滾動(dòng)超過(guò)一屏?xí)r,設(shè)置tab標(biāo)題滾動(dòng)條。
checkCor:function(){
if (this.data.currentTab>4){
this.setData({
scrollLeft:300
})
}else{
this.setData({
scrollLeft:0
})
}
},
onLoad: function() {
var that = this;
// 高度自適應(yīng)
wx.getSystemInfo( {
success: function( res ) {
var clientHeight=res.windowHeight,
clientWidth=res.windowWidth,
rpxR=750/clientWidth;
var calc=clientHeight*rpxR-180;
console.log(calc)
that.setData( {
winHeight: calc
});
}
});
},
footerTap:app.footerTap
})
三、wxss樣式
.tab-h{
height: 80rpx;width: 100%; box-sizing: border-box;overflow: hidden;line-height: 80rpx;background: #F7F7F7; font-size: 16px; white-space: nowrap;position: fixed;top: 0; left: 0; z-index: 99;}
.tab-item{margin:0 36rpx;display: inline-block;}
.tab-item.active{color: #4675F9;position: relative;}
.tab-item.active:after{ content: "";display: block;height: 8rpx;width: 52rpx;background: #4675F9;position: absolute; bottom: 0;left: 5rpx;border-radius: 16rpx;}
.item-ans{ width: 100%;display: flex; flex-grow: row no-wrap;justify-content: space-between; padding: 30rpx;box-sizing: border-box; height: 180rpx;align-items: center;border-bottom: 1px solid #F2F2F2;}
.avatar{width: 100rpx;height: 100rpx;position: relative;padding-right: 30rpx;}
.avatar .img{width: 100%;height: 100%;}
.avatar .doyen{width: 40rpx;height: 40rpx;position: absolute;bottom: -2px;right: 20rpx;}
.expertInfo{font-size: 12px;flex-grow: 2;color: #B0B0B0;line-height: 1.5em;}
.expertInfo .name{font-size: 16px;color:#000;margin-bottom: 6px;}
.askBtn{ width: 120rpx;height: 60rpx;line-height: 60rpx;text-align: center;font-size: 14px; border-radius: 60rpx;border: 1px solid #4675F9; color:#4675F9;}
.tab-content{margin-top: 80rpx;}
.scoll-h{height: 100%;}
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開(kāi)發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序開(kāi)發(fā)之實(shí)現(xiàn)選項(xiàng)卡(窗口頂部TabBar)頁(yè)面切換
- 微信小程序?qū)崿F(xiàn)選項(xiàng)卡功能
- 微信小程序 選項(xiàng)卡的簡(jiǎn)單實(shí)例
- 微信小程序的tab選項(xiàng)卡的實(shí)現(xiàn)效果
- 微信小程序開(kāi)發(fā)之選項(xiàng)卡(窗口底部TabBar)頁(yè)面切換
- 微信小程序 tabs選項(xiàng)卡效果的實(shí)現(xiàn)
- 微信小程序 實(shí)現(xiàn)tabs選項(xiàng)卡效果實(shí)例代碼
- 微信小程序?qū)崿F(xiàn)頂部選項(xiàng)卡(swiper)
- 微信小程序?qū)崙?zhàn)之頂部導(dǎo)航欄(選項(xiàng)卡)(1)
- 微信小程序?qū)崿F(xiàn)導(dǎo)航欄選項(xiàng)卡效果
相關(guān)文章
Bootstrap下拉菜單更改為懸停(hover)觸發(fā)的方法
這篇文章主要為大家詳細(xì)介紹了Bootstrap下拉菜單更改為懸停(hover)觸發(fā)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
使用Javascript監(jiān)控前端相關(guān)數(shù)據(jù)的代碼
本篇文章詳細(xì)的介紹了使用Javascript監(jiān)控前端相關(guān)數(shù)據(jù),可以及時(shí)的監(jiān)控前端的錯(cuò)誤,加載時(shí)間等,有需要的可以了解一下。2016-10-10
JavaScript數(shù)組實(shí)例的9個(gè)方法
這篇文章主要介紹了JavaScript數(shù)組實(shí)例的9個(gè)方法,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹沒(méi)具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
JS+HTML實(shí)現(xiàn)經(jīng)典游戲吃豆人
吃豆游戲可以說(shuō)是我們80,90后共同的回憶錄,小時(shí)候常常在學(xué)習(xí)機(jī)上玩,所以也就有了強(qiáng)烈的欲望去寫(xiě)。所以本文將利用JS+HTML實(shí)現(xiàn)這一經(jīng)典游戲,需要的可以參考一下2022-04-04
點(diǎn)擊彈出層效果&彈出窗口后網(wǎng)頁(yè)背景變暗效果的實(shí)現(xiàn)代碼
本篇文章主要是對(duì)點(diǎn)擊彈出層效果&彈出窗口后網(wǎng)頁(yè)背景變暗效果的實(shí)現(xiàn)代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02
JavaScript暫停和繼續(xù)定時(shí)器的實(shí)現(xiàn)方法
這篇文章主要介紹了JavaScript暫停和繼續(xù)定時(shí)器的方法的相關(guān)資料,非常不錯(cuò),需要的朋友可以參考下2016-07-07

