在微信小程序中,使用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锚点跳转类似,如需要更加复杂的业务逻辑,往里面加即可,大概架子就是这样

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐