小程序之scroll-into-view, wx.pageScrollTo首次load沒有效果

scroll-view用法查看官方文檔

<scroll-view scroll-y scroll-into-view="{{toView}}" style="height: 700px">
    <view wx:for="{{ list }}" wx:for-item="category" wx:key="name" id="type{{index}}">
      <view class="type">
        <text class="type-name">{{ category.name }}</text>
        <text class="type-description">{{ category.description }}</text>
      </view>
    </view>
  </scroll-view>

列表渲染,同時給需要滑動到的view加id (type0, type1, ….)
js:

 data: {
    list: [],
    toView: 'type1',
  },
  onLoad: function () {
    const list = app.list('products');
    this.setData({
      list,
    });
  },

列表渲染,數據在onLoad這個生命週期函數裏setData。結果並沒有滑動到指定id的view位置。

原因是列表數據在onLoad裏纔拿到,初始化的時候list是空的,scroll-into-view滾不起來,所以就沒有效果了。

解決:

onLoad: function () {
    const list = app.list('products')
    const { solutionId } = app.globalData
    this.setData({
      list,
      toView: `type${solutionId}`
    })
 },

將toView更新就OK
當然也不一定都在onLoad裏,要根據實際情況。

wx.pageScrollTo也是這樣,應該很多相關的問題都是這個道理。這個坑,記!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章