课程表

微信小程序框架

微信小程序组件

微信小程序API

微信小程序开放接口

微信小程序工具

微信小程序设计规范

工具箱
速查手册

小程序 可滚动视图区域

当前位置:免费教程 » 移动开发 » 微信小程序

scroll-view


可滚动视图区域。

属性名 类型 默认值 说明
scroll-x Boolean false 允许横向滚动
scroll-y Boolean false 允许纵向滚动
upper-threshold Number 50 距顶部/左边多远时(单位px),触发 scrolltoupper 事件
lower-threshold Number 50 距底部/右边多远时(单位px),触发 scrolltolower 事件
scroll-top Number   设置竖向滚动条位置
scroll-left Number   设置横向滚动条位置
scroll-into-view String   值应为某子元素id(id不能以数字开头),则滚动到该元素,元素顶部对齐滚动区域顶部,只支持纵向滚动
scroll-with-animation Boolean false 在设置滚动条位置时使用动画过渡
enable-back-to-top Boolean false iOS点击顶部状态栏、安卓双击标题栏时,滚动条返回顶部,只支持竖向
bindscrolltoupper EventHandle   滚动到顶部/左边,会触发 scrolltoupper 事件
bindscrolltolower EventHandle   滚动到底部/右边,会触发 scrolltolower 事件
bindscroll EventHandle   滚动时触发,event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY}

使用竖向滚动时,需要给<scroll-view/>一个固定高度,通过 WXSS 设置 height。

示例代码:

  1. <view class="section">
  2. <view class="section__title">vertical scroll</view>
  3. <scroll-view scroll-y="true" style="height: 200px;" bindscrolltoupper="upper" bindscrolltolower="lower" bindscroll="scroll" scroll-into-view="{{toView}}" scroll-top="{{scrollTop}}">
  4. <view id="green" class="scroll-view-item bc_green"></view>
  5. <view id="red" class="scroll-view-item bc_red"></view>
  6. <view id="yellow" class="scroll-view-item bc_yellow"></view>
  7. <view id="blue" class="scroll-view-item bc_blue"></view>
  8. </scroll-view>
  9.  
  10. <view class="btn-area">
  11. <button size="mini" bindtap="tap">click me to scroll into view </button>
  12. <button size="mini" bindtap="tapMove">click me to scroll</button>
  13. </view>
  14. </view>
  15. <view class="section section_gap">
  16. <view class="section__title">horizontal scroll</view>
  17. <scroll-view class="scroll-view_H" scroll-x="true" style="width: 100%">
  18. <view id="green" class="scroll-view-item_H bc_green"></view>
  19. <view id="red" class="scroll-view-item_H bc_red"></view>
  20. <view id="yellow" class="scroll-view-item_H bc_yellow"></view>
  21. <view id="blue" class="scroll-view-item_H bc_blue"></view>
  22. </scroll-view>
  23. </view>
  1. var order = ['red', 'yellow', 'blue', 'green', 'red']
  2. Page({
  3. data: {
  4. toView: 'red',
  5. scrollTop: 100
  6. },
  7. upper: function(e) {
  8. console.log(e)
  9. },
  10. lower: function(e) {
  11. console.log(e)
  12. },
  13. scroll: function(e) {
  14. console.log(e)
  15. },
  16. tap: function(e) {
  17. for (var i = 0; i < order.length; ++i) {
  18. if (order[i] === this.data.toView) {
  19. this.setData({
  20. toView: order[i + 1]
  21. })
  22. break
  23. }
  24. }
  25. },
  26. tapMove: function(e) {
  27. this.setData({
  28. scrollTop: this.data.scrollTop + 10
  29. })
  30. }
  31. })

scroll-view

 

Bug & Tip

  1. tip: 请勿在 scroll-view 中使用 textarea、map、canvas、video 组件
  2. tip: scroll-into-view 的优先级高于 scroll-top
  3. tip: 在滚动 scroll-view 时会阻止页面回弹,所以在 scroll-view 中滚动,是无法触发 onPullDownRefresh
  4. tip: 若要使用下拉刷新,请使用页面的滚动,而不是 scroll-view ,这样也能通过点击顶部状态栏回到页面顶部

 

 

 

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号