微信小程序計(jì)算scroll-view的高度

官方文檔開(kāi)頭就說(shuō),當(dāng)我們使用豎向滾動(dòng)時(shí),就需要給它一個(gè)固定高度,單位是px。

可滾動(dòng)視圖區(qū)域。使用豎向滾動(dòng)(scroll-y)時(shí),需要給scroll-view一個(gè)固定高度,通過(guò) WXSS 設(shè)置 height。組件屬性的長(zhǎng)度單位默認(rèn)為px,2.4.0起支持傳入單位(rpx/px)。微信開(kāi)放文檔

編寫(xiě)WXML,項(xiàng)目上將這段作為template:

<scroll-view
      scroll-y style="width: 100%;height:{{scrollHeight}}px;"
      refresher-enabled="{{true}}"
      refresher-threshold="{{100}}"
      refresher-default-style="white"
      refresher-background="lightgreen"
      refresher-triggered="{{triggered}}"
      bindrefresherpulling="onPulling"
      bindrefresherrefresh="onRefresh"
      bindrefresherrestore="onRestore"
      bindrefresherabort="onAbort"
  >
    <view>
      <block><!--wx:for,循環(huán)下面的列表內(nèi)容-->
        <view class="goods_item">
          <view class="goods_image"><image src="../../../images/ico_01.png" mode="widthFix"></image></view>
          <view class="goods_exp">
            <text class="goods_desc">商品描述-手機(jī)吧啦吧啦</text>
            <view class="goods_price">¥<text>1399.00</text></view>
          </view>
        </view>
      </block>
    </view>
  </scroll-view>

這時(shí)候我們需要在對(duì)應(yīng)的js中去計(jì)算這個(gè) scrollHeight

onLoad: function (options) {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        // console.info(res.windowHeight);
        let height = res.windowHeight;
        wx.createSelectorQuery().selectAll('.list_top').boundingClientRect(function (rects) {
          rects.forEach(function (rect) {
            that.setData({
              scrollHeight: height - rect.bottom
            });
          })
        }).exec();
      }
    });
  },

這里的list_top是上面的搜索欄,我們需要減去搜索欄的高度;
現(xiàn)在就已經(jīng)可以在界面上進(jìn)行滑動(dòng)了,添加高度完成。

·擴(kuò)展一下
scroll-view下拉刷新相關(guān)的操作,完整的js文件

  data: {
    triggered: false,
    _freshing: false
  },
  onLoad: function (options) {
    //計(jì)算scrollHeight
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        // console.info(res.windowHeight);
        let height = res.windowHeight;
        wx.createSelectorQuery().selectAll('.list_top').boundingClientRect(function (rects) {
          rects.forEach(function (rect) {
            that.setData({
              scrollHeight: height - rect.bottom
            });
          })
        }).exec();
      }
    });
  },
  onReady: function () {
    //初始化
    setTimeout(() => {
      this.setData({
        triggered: true,
      })
    }, 1000)
  },
  //自定義下拉刷新控件被下拉
  onPulling(e) {
    console.log('onPulling:', e)
  },
  //自定義下拉刷新被觸發(fā)
  onRefresh() {
    if (this.data._freshing) return
    this.setData({
      _freshing: true
    })
    setTimeout(() => {
      this.setData({
        triggered: false,
        _freshing: false
      })
    }, 3000)
  },
  //自定義下拉刷新被復(fù)位
  onRestore(e) {
    console.log('onRestore:', e)
  },
  //自定義下拉刷新被中止
  onAbort(e) {
    console.log('onAbort', e)
  }

示例圖:

示例

問(wèn)題
根據(jù)上述代碼,下拉實(shí)現(xiàn)了,但是不能復(fù)位,測(cè)試發(fā)現(xiàn) bindrefresherrestore 未執(zhí)行,也就是 onRestore 方法未實(shí)現(xiàn)。
經(jīng)過(guò)測(cè)試發(fā)現(xiàn)問(wèn)題出在模板上,需要將data數(shù)據(jù)通過(guò)template標(biāo)簽的data屬性傳進(jìn)去:

onRestore

tips:如果你的項(xiàng)目上沒(méi)有使用template,那么就可以忽略這個(gè)問(wèn)題。

參考文檔:
微信開(kāi)放文檔:scroll-view
https://developers.weixin.qq.com/miniprogram/dev/component/scroll-view.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容