欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序商城分類滾動(dòng)列表錨點(diǎn)的項(xiàng)目實(shí)踐

 更新時(shí)間:2023年04月19日 10:22:25   作者:絲網(wǎng)如風(fēng)  
本文主要介紹了微信小程序商城分類滾動(dòng)列表錨點(diǎn)的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、需求背景

最近接了個(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)

二、效果演示

錄制_2023_04_18_11_25_56_701.gif

三、核心代碼實(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

image.png

        <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

image.png

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論