scroll-view

可滚动视图区域。

属性名类型默认值说明最低版本
scroll-xBooleanfalse设置为横向滚动
scroll-yBooleanfalse设置为竖向滚动
upper-thresholdNumber50距顶部/左边多远时(单位px),触发 scrolltoupper 事件
lower-thresholdNumber50距底部/右边多远时(单位px),触发 scrolltolower 事件
scroll-topNumber设置竖向滚动条位置
scroll-leftNumber设置横向滚动条位置
scroll-into-viewString值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素
scroll-with-animationBooleanfalse在设置滚动条位置时使用动画过渡
bindscrollEventHandler滚动时触发
bindscrolltoupperEventHandler滚动到顶部/左边
bindscrolltolowerEventHandler滚动到底部/右边
- 使用竖向滚动时,需要给一个固定高度,通过 ttss 设置 height。 - scroll-into-view只能包含[-_a-zA-Z0-9]

示例

  1. <view class="page-section-title">
  2. <text>Vertical Scroll\n纵向滚动</text>
  3. </view>
  4. <scroll-view style="height: 300rpx;"
  5. scroll-y
  6. scroll-with-animation
  7. bindscrolltoupper="upper"
  8. bindscrolltolower="lower"
  9. bindscroll="scroll"
  10. scroll-into-view="{{toView}}"
  11. scroll-top="{{scrollTop}}"
  12. >
  13. <view id="demo1" class="scroll-view-item demo-text-1"></view>
  14. <view id="demo2" class="scroll-view-item demo-text-2"></view>
  15. <view id="demo3" class="scroll-view-item demo-text-3"></view>
  16. </scroll-view>
  17. <button bindtap="tap">Scroll into</button>
  18. <button bindtap="tapMove">Move</button>
  19. <view class="page-section-title">
  20. <text>Horizontal Scroll\n横向滚动</text>
  21. </view>
  22. <scroll-view class="scroll-view_H" scroll-x style="width: 100%">
  23. <view id="demo1" class="scroll-view-item_H demo-text-1"></view>
  24. <view id="demo2" class="scroll-view-item_H demo-text-2"></view>
  25. <view id="demo3" class="scroll-view-item_H demo-text-3"></view>
  26. </scroll-view>
  1. var order = ['demo1', 'demo2', 'demo3']
  2. Page({
  3. data: {
  4. toView: 'demo1',
  5. scrollTop: 0,
  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 < order.length - 1 ? i + 1 : 0],
  21. })
  22. break
  23. }
  24. }
  25. },
  26. tapMove: function(e) {
  27. this.setData({
  28. scrollTop: this.data.scrollTop + 20
  29. })
  30. }
  31. })

scroll-view - 图1

原文: https://developer.toutiao.com/docs/comp/scroll-view.html