微信小程序商城分類滾動(dòng)列表錨點(diǎn)的項(xiàng)目實(shí)踐
一、需求背景
最近接了個(gè)商城小程序的項(xiàng)目,在做商品分類頁(yè)面的時(shí)候,一開始是普通分類列表,但是客戶覺得效果不理想,想要滾動(dòng)列表的效果,需要實(shí)現(xiàn)以下功能:
- 列表滑動(dòng)效果;
- 滑動(dòng)切換分類;
- 點(diǎn)擊分類跳轉(zhuǎn)到相應(yīng)的分類位置。
思路是用使用官方組件scroll-view,給每個(gè)分類(子元素)添加錨點(diǎn),然后記錄每個(gè)分類項(xiàng)的高度,監(jiān)聽scroll-view組件滾動(dòng)事件,計(jì)算分類的跳轉(zhuǎn)
二、效果演示
三、核心代碼實(shí)現(xiàn)
下面要使用到的方法都來自于查閱微信小程序官方文檔
創(chuàng)建一個(gè)scoll-view 并配置需要用到的屬性scroll-into-view根據(jù)文檔描述此屬性是子元素的id,值為哪個(gè)就跳到那個(gè)子元素。為了使跳轉(zhuǎn)不顯得生硬,再添加scroll-with-animation屬性,然后創(chuàng)建動(dòng)態(tài)生成分類的dom元素并為每個(gè)子元素添加相應(yīng)的id
<view class="content"> <!-- 左側(cè)分類 --> <scroll-view scroll-y scroll-with-animation class="left" style="height:{{height}}rpx;" scroll-into-view='{{leftId}}'> <view id='left{{index}}' class="left-item {{activeKey===index?'active':''}}" wx:for="{{navData}}" data-index='{{index}}' wx:key='id' bindtap="onChange"> <text class='name'>{{item.name}}</text> </view> </scroll-view> <!-- 滾動(dòng)列表 --> <scroll-view class="right" scroll-y scroll-with-animation scroll-into-view="{{selectedId}}" bindscroll="changeScroll" style='height:{{height}}rpx;'> <!-- 每個(gè)分類 --> <view class="item" wx:for="{{goodslist}}" wx:key="id" id='type{{index}}'> <!-- 分類標(biāo)題 --> <view class="type">【{{item.name}}】</view> <!-- 分類下的商品 --> <view class="item-list"> <navigator class="list-item" wx:for="{{item.list}}" wx:for-item='key' wx:key="id" url='/pages/goods/goods?id={{key.id}}'> <image style="width: 100%; height: 180rpx;" src="{{key.imgurl}}" /> <view class="item-name">{{key.goods_name}}</view> </navigator> </view> <view wx:if="{{item.list.length===0}}" class="nodata"> 暫無商品 </view> </view> </scroll-view> </view>
css部分
這里用到了吸頂效果position: sticky;
.content { width: 100%; height: calc(100% - 108rpx); overflow-y: hidden; display: flex; .left { height: 100%; overflow-y: scroll; .left-item { width: 100%; padding: 20rpx; box-sizing: border-box; .name { word-wrap: break-word; font-size: 28rpx; color: #323233; } } .active { border-left: 6rpx #ee0a24 solid; background-color: #fff; } } .right { flex: 1; .item { position: relative; padding: 20rpx; .type { margin-bottom: 10rpx; padding: 5rpx; position: sticky; top: 0; background-color: #fff; } .item-list { width: 100%; display: grid; grid-template-columns: 1fr 1fr 1fr; grid-gap: 20rpx; text-align: center; .item-name { color: #3a3a3a; font-size: 26rpx; margin-top: 10rpx; } } .nodata{ padding: 20rpx; color: #ccc; } } } }
2. 在列表渲染完成之后計(jì)算出每個(gè)分類的高度并且保存成一個(gè)數(shù)組
// 用到的data data:{ // 分類列表 navData:[], // 商品列表 goodslist:[], // 左側(cè)分類選中項(xiàng) 分類列表數(shù)組的下標(biāo) activeKey:0, // 計(jì)算出的錨點(diǎn)的位置 heightList:[], // 右側(cè)子元素的錨點(diǎn) selectedId: 'type0', // 左側(cè)分類的錨點(diǎn) leftId:'left0', // scroll-view 的高度 height:0 }, onShow() { let Height = 0; wx.getSystemInfo({ success: (res) => { Height = res.windowHeight } }) const query = wx.createSelectorQuery(); query.selectAll('.search').boundingClientRect() query.exec((res) => { // 計(jì)算滾動(dòng)列表的高度 視口高度減去頂部高度 *2是因?yàn)槟玫降氖莗x 雖然也可以 但是我們通常使用的是rpx this.setData({ height: (Height - res[0][0].height) * 2 }) }) }, //計(jì)算右側(cè)每個(gè)錨點(diǎn)的高距離頂部的高 selectHeight() { let h = 0; const query = wx.createSelectorQuery(); query.exec((res) => { console.log('res', res) let arr=res[0].map((item,index)=>{ h+ = item.height return h }) this.setData({ heightList: arr, }) console.log('height', this.data.heightList) }) },
使用到的相關(guān)API
3.監(jiān)聽scroll-view的滾動(dòng)事件,通過滾動(dòng)位置計(jì)算當(dāng)前是哪個(gè)分類。
changeScroll(e) { // 獲取距離頂部的距離 let scrollTop = e.detail.scrollTop; // 當(dāng)前分類選中項(xiàng),分類列表下標(biāo) let {activeKey,heightList} = this.data; // 防止超出分類 判斷滾動(dòng)距離是否超過當(dāng)前分類距離頂部高度 if (activeKey + 1 < heightList.length && scrollTop >= heightList[activeKey]) { this.setData({ // 左側(cè)分類選中項(xiàng)改變 activeKey: activeKey + 1, // 左側(cè)錨點(diǎn)對(duì)應(yīng)位置 leftId: `left${activeKey + 1}` }) } if (activeKey - 1 >= 0 && scrollTop < heightList\[activeKey - 1]) { this.setData({ activeKey: activeKey - 1, leftId: `left${activeKey - 1}` }) } },
4. 監(jiān)聽分類列表點(diǎn)擊事件,點(diǎn)擊分類跳轉(zhuǎn)相應(yīng)的分類商品列表
onChange(event) { let index = event.currentTarget.dataset.index this.setData({ activeKey: index, selectId: "item" + index }); },
四、總結(jié)
左側(cè)分類一開始是用的vantUI的滾動(dòng)列表,但是分類過多就不會(huì)隨著滑動(dòng)分類滾動(dòng)到可視位置,所以改成自定義組件,反正也很簡(jiǎn)單。
最初是想根據(jù)右側(cè)滾動(dòng)位置給左側(cè)的scroll-view添加scroll-top,雖然實(shí)現(xiàn),但是有時(shí)會(huì)有一點(diǎn)小問題目前沒有想好怎么解決,改用右側(cè)相同方法實(shí)現(xiàn)可以解決。
css部分使用scss編寫,使用的是vscode的easy scss插件,具體方法百度一下,很簡(jiǎn)單。
到此這篇關(guān)于微信小程序商城分類滾動(dòng)列表錨點(diǎn)的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)小程序滾動(dòng)列表錨點(diǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 微信小程序?qū)崿F(xiàn)錨點(diǎn)定位功能的方法實(shí)例
- 微信小程序?qū)崿F(xiàn)錨點(diǎn)跳轉(zhuǎn)
- 微信小程序scroll-view實(shí)現(xiàn)滾動(dòng)到錨點(diǎn)左側(cè)導(dǎo)航欄點(diǎn)餐功能(點(diǎn)擊種類,滾動(dòng)到錨點(diǎn))
- 微信小程序 scroll-view 實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)功能
- 微信小程序scroll-view錨點(diǎn)鏈接滾動(dòng)跳轉(zhuǎn)功能
- 微信小程序?qū)崿F(xiàn)錨點(diǎn)功能
- 小程序?qū)崿F(xiàn)錨點(diǎn)滑動(dòng)效果
- 微信小程序 scroll-view實(shí)現(xiàn)錨點(diǎn)滑動(dòng)的示例
- 微信小程序?qū)崿F(xiàn)錨點(diǎn)定位樓層跳躍的實(shí)例
相關(guān)文章
javascript輕量級(jí)庫(kù)createjs使用Easel實(shí)現(xiàn)拖拽效果
這篇文章主要介紹了javascript輕量級(jí)庫(kù)createjs使用Easel實(shí)現(xiàn)拖拽效果的相關(guān)資料,需要的朋友可以參考下2016-02-02JS函數(shù)(普通函數(shù),箭頭函數(shù))中this的指向問題詳解
這篇文章主要給大家介紹了JS中普通函數(shù)和箭頭函數(shù)的this指向,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09JS前端知識(shí)點(diǎn)offset,scroll,client,冒泡,事件對(duì)象的應(yīng)用整理總結(jié)
這篇文章主要介紹了JS前端知識(shí)點(diǎn)offset,scroll,client,冒泡,事件對(duì)象的應(yīng)用,結(jié)合實(shí)例形式總結(jié)分析了offset,scroll,client,冒泡,事件對(duì)象相關(guān)功能、原理及操作注意事項(xiàng),需要的朋友可以參考下2019-06-06JavaScript實(shí)現(xiàn)表格表單的隨機(jī)選擇和簡(jiǎn)單的隨機(jī)點(diǎn)名
本文主要介紹了JavaScript實(shí)現(xiàn)表格表單的隨機(jī)選擇和簡(jiǎn)單的隨機(jī)點(diǎn)名,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08給Flash加一個(gè)超鏈接(推薦使用透明層)兼容主流瀏覽器
給一個(gè)Flash加一個(gè)超鏈接,原想直接在object外直接套一個(gè)超鏈接即可,試了之后卻發(fā)現(xiàn)不是這么回事2013-06-06JavaScript 中使用Promise.all()方法經(jīng)驗(yàn)技巧詳解
這篇文章主要為大家介紹了JavaScript 中使用Promise.all()方法經(jīng)驗(yàn)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10JS庫(kù)particles.js創(chuàng)建超炫背景粒子插件(附源碼下載)
particles.js用于創(chuàng)建粒子的輕量級(jí) JavaScript 庫(kù)。使用方法非常簡(jiǎn)單,代碼也很容易實(shí)現(xiàn),下面通過本文給大家分享JS庫(kù)particles.js創(chuàng)建超炫背景粒子插件附源碼下載,需要的朋友參考下吧2017-09-09JavaScript檢測(cè)用戶是否在線的6種方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了JavaScript中實(shí)現(xiàn)檢測(cè)用戶是否在線的6種常用方法,文中的示例代碼講解詳細(xì),感興趣的可以跟隨小編一起學(xué)習(xí)一下2023-08-08