微信小程序滾動Tab實現左右可滑動切換
效果:

最終效果如上。問題: 
1、tab標題總共8個,所以一屏無法全部顯示。 
2、tab內容區左右滑動切換時,tab標題隨即做標記(active)。 
3、當active的標題不在當前屏顯示時,要使其能顯示到當前屏中。
一、wxml結構
tab標題因一排八個,所以使用 scroll-view組件,使其可橫向滾動。 
tab內容可左右滑動切換,使用swiper組件實現 
為了偷懶,所以數據都通過wx:for遍歷重復出來。
說明:
1、設置data-current屬性用于:點擊當前項時,通過點擊事件swichNav中處理e.dataset.current取到點擊的目標值。 
2、swiper組件的current組件用于控制當前顯示哪一頁 
3、swiper組件綁定change事件switchTab,通過e.detail.current拿到當前頁
<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">職場</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="http://ookzqad11.bkt.clouddn.com/avatar.png"></image>            </view>            <view class="expertInfo">              <view class="name">歡顏</view>              <view class="tag">知名情感博主</view>              <view class="answerHistory">134個回答,2234人聽過 </view>            </view>            <navigator url="/pages/askExpert/expertDetail" class="askBtn">問TA</navigator>           </view>        </block>      </scroll-view>    </swiper-item>  </swiper></view>二、js部分
微信小程序在開發起來,個人感覺挺像vue的,以數據驅動視圖的更新。所以在小程序中,不能直接操作dom,當然也不能使用jquery之類的庫。
var app = getApp();Page({  data:{    winHeight:"",//窗口高度    currentTab:0, //預設當前項的值    scrollLeft:0, //tab標題的滾動條位置    expertList:[{ //假數據      img:"avatar.png",      name:"歡
主站蜘蛛池模板:
元谋县|
望谟县|
迁西县|
枣强县|
贵溪市|
连城县|
敦煌市|
大庆市|
视频|
新郑市|
留坝县|
庆阳市|
六枝特区|
卓尼县|
陇川县|
和硕县|
织金县|
怀集县|
蓬莱市|
航空|
得荣县|
京山县|
兴和县|
肇庆市|
伽师县|
平和县|
错那县|
清镇市|
宜兰市|
团风县|
越西县|
深圳市|
云安县|
葫芦岛市|
临安市|
翁牛特旗|
罗平县|
吉林市|
宜宾县|
文化|
合水县|