微信小程序滾動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:"歡 主站蜘蛛池模板: 罗源县| 镇宁| 赤水市| 开江县| 沛县| 诸城市| 伊金霍洛旗| 泾川县| 桓台县| 恩施市| 安陆市| 江安县| 边坝县| 红河县| 聂拉木县| 清涧县| 巫溪县| 桐庐县| 衡水市| 思茅市| 凤台县| 花莲市| 黄浦区| 九江市| 龙里县| 深泽县| 富阳市| 建湖县| 顺昌县| 延津县| 龙山县| 莱阳市| 巴林右旗| 徐闻县| 伊川县| 方城县| 安顺市| 稷山县| 郸城县| 赫章县| 武胜县|