您好,登錄后才能下訂單哦!
本篇內容主要講解“微信小程序實現滑動/點擊切換Tab及scroll-left的使用方法是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“微信小程序實現滑動/點擊切換Tab及scroll-left的使用方法是什么”吧!
scroll-view:
可滾動視圖區域。使用豎向滾動時,需要給scroll-view一個固定高度,通過 WXSS 設置 height。組件屬性的長度單位默認為px。
scroll-x(boolean):允許橫向滾動
scroll-y(boolean):允許縱向滾動
scroll-left(number/string):設置橫向滾動條位置
scroll-with-animation(boolean):在設置滾動條位置時使用動畫過渡
定義一個tab列表,scroll-view包裹,允許橫向滾動,設置scroll-left默認為0
每個tab設置為display: inline-block,scroll-view設置 white-space: nowrap不換行
<scroll-view scroll-x class="container-head-sc" scroll-left="{{sleft}}" scroll-with-animation="true"> <view class="item" wx:key="list" wx:for="{{list}}" wx:for-index="index">tab-{{index+1}} </view> </scroll-view>
.container-head-sc { height: 50rpx; border-radius: 25rpx; background: #eeece4; color: #333; white-space: nowrap; } .container-head-sc .item { padding: 0 20rpx; min-width: 90rpx; text-align: center; line-height: 50rpx; font-size: 26rpx; display: inline-block; height: 50rpx; }
給每個tab設置vertical-align: top;防止高度塌陷
.container-head-sc .item{ /* 防止高度塌陷 */ + vertical-align: top; }
添加當前激活tab樣式,定義當前選中項索引currentTab默認為0(即選中第一個),當currentTab==列表的某一項索引表示選中
<view class="item {{currentTab == index ?'active':''}}" data-current="{{index}}" catchtap="handleTabChange" wx:key="list" wx:for="{{list}}" wx:for-index="index">tab-{{index+1}} </view>
.container-head-sc .active { color: #ffffff; font-weight: bold; background: orange; border-radius: 25rpx; }
添加切換事件
handleTabChange(e) { let { current } = e.target.dataset; if (this.data.currentTab == current || current === undefined) return; this.setData({ currentTab: current, }); },
swiper:
滑塊視圖容器。默認高度為150px;
current(number):當前所在滑塊的 index,默認為0
autoplay(boolean):是否自動切換
bindchange(eventhandle):current 改變時會觸發 change 事件,event.detail = {current, source}
swiper包裹內容列表,需要為swiper指定高度,這里我們設置為撐滿一屏
/* swiper默認高度為150px */ .container-swiper { height: calc(100% - 110rpx); }
設置swiper的current為當前選中的tab標簽索引,即currentTab
<swiper current="{{currentTab}}" class="container-swiper"> <swiper-item class="flex-column j_c" wx:for="{{list}}" wx:key='index'> </swiper-item> </swiper>
swiper-item展示內容列表,用scroll-view包裹內容,設置豎向滾動,使用豎向滾動時,需要給scroll-view一個固定高度,這里將scroll-view高度設置為100%,與swiper同高,鋪滿一屏
<swiper-item class="flex-column j_c" wx:for="{{list}}" wx:key='index'> <scroll-view scroll-y class="container-swiper-sc"> <view class="flex-wrap flex-row items"> ....//內容 </view> </scroll-view> </swiper-item>
.container-swiper-sc { height: 100%; }
swiper添加bindchange事件,當滑動時候,動態的設置currentTab,實現tab列表的同步更新
<swiper current="{{currentTab}}" bindchange="handleSwiperChange" class="container-swiper"> ....//內容 </swiper>
handleSwiperChange(e) { this.setData({ currentTab: e.detail.current, }); },
可以發現,當swiper所在滑塊的 index超出tab列表的可視范圍,我們得手動滑動tab列表才能看見當前所選中的tab
找到2.1節 scroll-left=“{{sleft}}”,scroll-left用來設置橫向滾動條位置,也就是說,我們可以監聽swiper的滾動,在滑塊所在的index改變的時候,去動態的設置scroll-left的位置
scroll-left的計算
wx.createSelectorQuery():
返回一個 SelectorQuery 對象實例
SelectorQuery.selectAll(string selector):
在當前頁面下選擇匹配選擇器 selector 的所有節點。
getScrollLeft() { const query = wx.createSelectorQuery(); query.selectAll(".item").boundingClientRect(); //這里將會返回頁面中所有class為item的節點,個數為tab列表的長度 query.exec((res) => { let num = 0; for (let i = 0; i < this.data.currentTab; i++) { num += res[0][i].width; } // 計算當前currentTab之前的寬度總和 this.setData({ sleft: Math.ceil(num), }); }); },
修改swiper的bindchange事件,每次滑塊的變化,都重新計算scroll-left的大小
handleSwiperChange(e) { + this.getScrollLeft(); },
<view class="head flex-row"> <view class="head-title">scroll-left</view> </view> <scroll-view scroll-y class="container"> <view class="container-head flex-row"> <scroll-view scroll-x class="container-head-sc" scroll-left="{{sleft}}" scroll-with-animation="true"> <view class="item {{currentTab == index ?'active':''}}" data-current="{{index}}" catchtap="handleTabChange" wx:key="list" wx:for="{{list}}" wx:for-index="index">tab-{{index+1}} </view> </scroll-view> </view> <swiper current="{{currentTab}}" bindchange="handleSwiperChange" class="container-swiper"> <swiper-item class="flex-column j_c" wx:for="{{list}}" wx:key='index'> <scroll-view scroll-y class="container-swiper-sc"> <view class="flex-wrap flex-row items"> <block wx:for="{{item}}" wx:key="index"> <image src="https://i.postimg.cc/mgsKJGLw/susu1.jpg" mode="aspectFill" class="item-img" /> </block> </view> </scroll-view> </swiper-item> </swiper> </scroll-view>
page { background-color: #ffa500; height: 100%; } .head { height: 90rpx; color: #333; font-size: 30rpx; padding-left: 30rpx; font-weight: bold; padding-bottom: 10rpx; box-sizing: border-box; } .head-title { position: relative; display: inline-block; height: 100%; } .head-title::after { content: ''; position: absolute; z-index: 99; width: 15px; height: 15px; margin-left: -15rpx; border-top: 3px solid #333; border-right: 3px solid #333; border-top-right-radius: 100%; transform: rotate(-225deg); left: 50%; bottom: 3px; } .container { width: 100%; height: calc(100% - 90rpx); background-color: #fff; overflow: hidden; border-radius: 30rpx 30rpx 0 0; } .container-head { width: 100%; height: 110rpx; box-sizing: border-box; padding: 10rpx 20rpx; } .container-head-sc { height: 50rpx; border-radius: 25rpx; background: #eeece4; color: #333; white-space: nowrap; } .container-head-sc .item { padding: 0 20rpx; min-width: 90rpx; text-align: center; line-height: 50rpx; font-size: 26rpx; display: inline-block; /* 引起高度塌陷 */ vertical-align: top; height: 50rpx; } .container-head-sc .active { color: #ffffff; font-weight: bold; background: orange; border-radius: 25rpx; } /* swiper默認高度為150px */ .container-swiper { height: calc(100% - 110rpx); } .container-swiper-sc { height: 100%; } .container-swiper-sc .items { padding: 0 2%; width: 100%; box-sizing: border-box; } .container-swiper-sc .items .item-img { width: 30vw; height: 30vw; margin-right: 2.8%; margin-bottom: 10rpx; flex-shrink: 0; } .container-swiper-sc .items .item-img:nth-child(3n+3) { margin-right: 0; } /* 隱藏scroll-view的滾動條 */ ::-webkit-scrollbar { width: 0; height: 0; color: transparent; }
Page({ data: { currentTab: 0, sleft: "", //橫向滾動條位置 list: [1, 2, 3, 4, 5, 6, 7, 22, 32],//測試列表 }, handleTabChange(e) { let { current } = e.target.dataset; if (this.data.currentTab == current || current === undefined) return; this.setData({ currentTab: current, }); }, handleSwiperChange(e) { this.setData({ currentTab: e.detail.current, }); this.getScrollLeft(); }, getScrollLeft() { const query = wx.createSelectorQuery(); query.selectAll(".item").boundingClientRect(); query.exec((res) => { let num = 0; for (let i = 0; i < this.data.currentTab; i++) { num += res[0][i].width; } this.setData({ sleft: Math.ceil(num), }); }); }, });
到此,相信大家對“微信小程序實現滑動/點擊切換Tab及scroll-left的使用方法是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。