swan.pageScrollTo

解释:将页面滚动到目标位置(可以设置滚动动画时长)。

方法参数

Object object

object参数说明

示例

扫码体验

请使用百度APP扫码

代码示例1 - 页面滚动到顶部

在开发者工具中预览效果

  1. <view class="container"> <image class="image" src="https://b.bdstatic.com/miniapp/images/longImage.png"></image> <view class="swan-security-padding-bottom flex-button"> <button type="primary" bindtap="scrollToTop">返回顶部</button> </view></view>
  1. Page({ scrollToTop() { swan.pageScrollTo({ scrollTop: 0, duration: 300, success: res => { console.log('pageScrollTo success', res); }, fail: err => { console.log('pageScrollTo fail', err); } }); }});

代码示例2 - 页面滚动到底部

在开发者工具中预览效果

  1. <view class="container"> <image class="image" src="https://b.bdstatic.com/miniapp/images/longImage.png"></image> <view class="swan-security-padding-bottom flex-button"> <button type="primary" bindtap="scrollToBottom">滚动到页面底部</button> </view></view>
  1. Page({ scrollToBottom(){ swan.createSelectorQuery() .select(".image") .boundingClientRect(function(rect) { swan.pageScrollTo({ scrollTop: rect.bottom, duration: 300, success: res => { console.log('pageScrollTo success', res); }, fail: err => { console.log('pageScrollTo fail', err); } }); }) .exec(); }});

错误码

Android

iOS