将页面滚动到目标位置(pageScrollTo或scroll-view)

wx.pageScrollTo方法一:
wxml:

	<view style="background-color:#fff;width:100%;height:60vh">xv</view>
	<view id="moveThis" style="background-color:#eee;width:100%;height:60vh">xv</view>
	<view style="background-color:#ddd;width:100%;height:60vh">xv</view>
	<view style="background-color:#ccc;width:100%;height:60vh">xv</view>
	<view bindtap="moveTop">xxx</view>

js:

  data: {
    movethisTop:0
  },
   onLoad: function (options) {
    let that=this
    wx.createSelectorQuery().select('#moveThis').boundingClientRect(function(rect){
        that.data.movethisTop=rect.top
    }).exec();
  },
    moveTop:function(){
    let that=this
    wx.pageScrollTo({
      scrollTop:that.data.movethisTop,
      duration: 300
    })
  }

wx.pageScrollTo方法二:
wxml:

	<view style="background-color:#fff;width:100%;height:60vh">xv</view>
	<view id="moveThis" style="background-color:#eee;width:100%;height:60vh">xv</view>
	<view style="background-color:#ddd;width:100%;height:60vh">xv</view>
	<view style="background-color:#ccc;width:100%;height:60vh">xv</view>
	<view bindtap="moveTop">xxx</view>

js:

    moveTop:function(){
    wx.pageScrollTo({
      duration:300,
      selector:'#moveThis'
    })
  }

selector 语法(selector 基础库支持2.7.3开始支持)
selector类似于 CSS 的选择器,但仅支持下列语法。
ID选择器:#the-id
class选择器(可以连续指定多个):.a-class.another-class
子元素选择器:.the-parent > .the-child
后代选择器:.the-ancestor .the-descendant
跨自定义组件的后代选择器:.the-ancestor >>> .the-descendant
多选择器的并集:#a-node, .some-other-nodes

scroll-view方法一
wxml:

<scroll-view style="width:100%;height:800rpx;" scroll-top ="{{topNum}}" scroll-y="true">
<view style="width:100%;height:800rpx;background-color:#aaa"></view>
<view style="width:100%;height:800rpx;background-color:#bbb"></view>
<view style="width:100%;height:800rpx;background-color:#ccc"></view>
<view style="width:100%;height:800rpx;background-color:#ddd"></view>
<view style="width:100%;height:800rpx;background-color:#eee"></view>
<view style="width:100%;height:800rpx;background-color:#fff"></view>
<view bindtap="moveTop">xxxx</view>
<view style="width:100%;height:800rpx;background-color:#aaa"></view>
</scroll-view>

js:

  data: {
    topNum:0,
  },
    moveTop:function(){
      this.setData({
        topNum:this.data.topNum
      })
  }

scroll-view方法一
wxml:

<scroll-view style="width:100%;height:800rpx;" scroll-into-view="{{toView}}" scroll-with-animation='true'  scroll-y="true">
<view id="toView" style="width:100%;height:800rpx;background-color:#aaa"></view>
<view style="width:100%;height:800rpx;background-color:#bbb"></view>
<view style="width:100%;height:800rpx;background-color:#ccc"></view>
<view  bindtap="moveTop">xxxx</view>
<view style="width:100%;height:800rpx;background-color:#ddd"></view>
<view style="width:100%;height:800rpx;background-color:#eee"></view>
<view style="width:100%;height:800rpx;background-color:#fff"></view>
<view style="width:100%;height:800rpx;background-color:#aaa"></view>
</scroll-view>

js:

  moveTop:function(e){
   this.setData({
     toView:"toView"
   })
  },

你可能感兴趣的:(基础知识)