# wx.pageScrollTo(Object object)

基础库 1.4.0 开始支持,低版本需做兼容处理

Promise 风格 调用:支持

需要页面权限:当前是插件页面时,宿主小程序不能调用该接口,反之亦然

小程序插件:支持,需要小程序基础库版本不低于 2.1.0

在小程序插件中使用时,只能在当前插件的页面中调用

微信 Windows 版:支持

微信 Mac 版:支持

# 功能描述

将页面滚动到目标位置,支持选择器和滚动距离两种方式定位

# 参数

# Object object

属性 类型 默认值 必填 说明 最低版本
scrollTop number 滚动到页面的目标位置,单位 px
duration number 300 滚动动画的时长,单位 ms
selector string 选择器 2.7.3
offsetTop number 偏移距离,需要和 selector 参数搭配使用,可以滚动到 selector 加偏移距离的位置,单位 px 2.23.1
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

# selector 语法

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

# 示例代码

wx.pageScrollTo({
  scrollTop: 0,
  duration: 300
})