1. 程式人生 > >微信小程式 scroll-view 實現錨點跳轉

微信小程式 scroll-view 實現錨點跳轉

在微信小程式中,使用scroll-view實現長頁面的標記跳轉,官方文件中沒有例子演示,錨點標記主要是使用<scroll-view>的scroll-into-view屬性。

實現錨點跳轉主要以下幾點:

1,最外層容器使用滾動檢視 

2,賦值滾動到檢視,如:<scroll-view scroll-into-view =“{{toView}}”>

3,設定scroll-view滾動方向scroll-y =“true”

4,跳轉到的位置使用id(定位),如:<view id =“mark1”>

 

wxml程式碼 

<view class="list">
    <view bindtap=‘jumpTo‘ data-opt="list0">list0</view>
    <view bindtap=‘jumpTo‘ data-opt="list11">list11</view>
    <view bindtap=‘jumpTo‘ data-opt="list29">list29</view>
</view>


<scroll-view  scroll-into-view="{{toView}}"  scroll-y="true"   scroll-with-animation="true"   class="scr">
    <view wx:for="{{list}}"  id="{{item}}"  class="test">
      {{item}}
    </view>
</scroll-view>

wxss程式碼

.scr{
    position: relative;
    height: 500rpx
}
.test{
    height: 80rpx;
}
.list{ position: fixed; z-index: 9; top:30rpx; right: 10rpx; }

js程式碼

  data: {

    list: ["list0", "list1", "list2", "list3", "list4", "list5", "list11", "list12", "list13", "list14", "list15", "list25", "list26", "list27", "list28", "list29","list30"],
    toView: ‘‘
  },

  jumpTo: function (e) {

    // 獲取標籤元素上自定義的 data-opt 屬性的值
    let target = e.currentTarget.dataset.opt;

    this.setData({
      toView: target
    })
  }

小程式錨點跳轉和HTML錨點跳轉類似,如需要更加複雜的業務邏輯,往裡面加即可,大概架子就是這樣