ts-container-scroll.md 5.3 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4
# Scroll

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 该组件从API version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
5

Z
zengyawen 已提交
6 7 8

可滚动的容器组件,当子组件的布局尺寸超过父组件的视口时,内容可以滚动。

Z
zengyawen 已提交
9 10

## 权限列表
Z
zengyawen 已提交
11 12 13



Z
zengyawen 已提交
14 15

## 子组件
Z
zengyawen 已提交
16 17 18

支持单个子组件。

Z
zengyawen 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

## 接口

Scroll(scroller?: Scroller)


## 属性

| 名称 | 参数类型 | 默认值 | 描述 |
| -------- | -------- | -------- | -------- |
| scrollable | ScrollDirection | ScrollDirection.Vertical | 设置滚动方法。 |
| scrollBar | [BarState](ts-appendix-enums.md#barstate枚举说明) | ScrollDirection.Auto | 设置滚动条状态。 |
| scrollBarColor | Color | - | 设置滚动条的颜色。 |
| scrollBarWidth | Length | - | 设置滚动条的宽度。 |

- ScrollDirection枚举说明
  | 名称 | 描述 | 
  | -------- | -------- |
  | Horizontal | 仅支持水平方向滚动。 | 
  | Vertical | 仅支持竖直方向滚动。 | 
  | None | 不可滚动。 | 


## Scroller
Z
zengyawen 已提交
43 44 45

可滚动容器组件的控制器,可以将此组件绑定至容器组件,然后通过它控制容器组件的滚动,目前支持绑定到List和Scroll组件上。

Z
zengyawen 已提交
46 47

### 导入对象
Z
zengyawen 已提交
48 49 50 51 52 53

```
scroller: Scroller = new Scroller()
```


Z
zengyawen 已提交
54 55 56 57
### scroller.scrollTo

scrollTo(value: { xOffset: number | string, yOffset: number | string, animation?: { duration: number, curve: Curve } }): void

Z
zengyawen 已提交
58 59 60

滑动到指定位置。

Z
zengyawen 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73

- 参数
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 |
  | -------- | -------- | -------- | -------- | -------- |
  | xOffset | Length | 是 | - | 水平滑动偏移。 |
  | yOffset | Length | 是 | - | 竖直滑动偏移。 |
  | animation | {<br/>duration:&nbsp;number,<br/>curve:&nbsp;[Curve](ts-animatorproperty.md)&nbsp;\|<br/>CubicBezier&nbsp;\|<br/>SpringCurve<br/>} | 否 |  | 动画配置:<br/>-&nbsp;duration:&nbsp;滚动时长设置。<br/>-&nbsp;curve:&nbsp;滚动曲线设置。 |


### scroller.scrollEdge

scrollEdge(value: Edge): void

Z
zengyawen 已提交
74 75 76

滚动到容器边缘。

Z
zengyawen 已提交
77 78 79 80 81 82 83 84 85 86

- 参数
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | value | Edge | 是 | - | 滚动到的边缘位置。 | 


### scroller.scrollPage

scrollPage(value: { next: boolean, direction?: Axis }): void
Z
zengyawen 已提交
87 88

滚动到下一页或者上一页。
Z
zengyawen 已提交
89

Z
zengyawen 已提交
90 91 92 93 94 95 96 97 98 99
- 参数
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | next | boolean | 是 | - | 是否向下翻页。true表示向下翻页,false表示向上翻页。 |


### scroller.currentOffset

scroller.currentOffset(): Object

Z
zengyawen 已提交
100 101 102 103

返回当前的滚动偏移量。


Z
zengyawen 已提交
104 105 106 107 108
- 返回值
  | 类型 | 描述 | 
  | -------- | -------- |
  | {<br/>xOffset:&nbsp;number,<br/>yOffset:&nbsp;number<br/>} | xOffset:&nbsp;水平滑动偏移;<br/>yOffset:&nbsp;竖直滑动偏移。 | 

Z
zengyawen 已提交
109

Z
zengyawen 已提交
110
### scroller.scrollToIndex
Z
zengyawen 已提交
111

Z
zengyawen 已提交
112
scroller.scrollToIndex(value: number): void
Z
zengyawen 已提交
113 114 115 116


滑动到指定Index。

Z
zengyawen 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 仅支持list组件。


- 参数
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | value | number | 是 | - | 要滑动到的列表项在列表中的索引值。 | 


## 事件

| 名称 | 功能描述 | 
| -------- | -------- | -------- |
| onScroll(xOffset:&nbsp;number,&nbsp;yOffset:&nbsp;number)&nbsp;=&gt;&nbsp;void | 滚动事件回调,&nbsp;返回滚动时水平、竖直方向偏移量。 | 
| onScrollEdge(side:&nbsp;Edge)&nbsp;=&gt;&nbsp;void | 滚动到边缘事件回调。 | 
| onScrollEnd()&nbsp;=&gt;&nbsp;void | 滚动停止事件回调。 | 


## 示例
Z
zengyawen 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

```
@Entry
@Component
struct ScrollExample {
  scroller: Scroller = new Scroller()
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  build() {
    Stack({ alignContent: Alignment.TopStart }) {
      Scroll(this.scroller) {
        Column() {
          ForEach(this.arr, (item) => {
            Text(item.toString())
              .width('90%').height(150).backgroundColor(0xFFFFFF)
              .borderRadius(15).fontSize(16).textAlign(TextAlign.Center)
              .margin({ top: 10 })
          }, item => item)
        }.width('100%')
      }
      .scrollable(ScrollDirection.Vertical).scrollBar(BarState.On)
      .scrollBarColor(Color.Gray).scrollBarWidth(30)
      .onScroll((xOffset: number, yOffset: number) => {
        console.info(xOffset + ' ' + yOffset)
      })
      .onScrollEdge((side: Edge) => {
        console.info('To the edge')
      })
      .onScrollEnd(() => {
        console.info('Scroll Stop')
      })

      Button('scroll 100')
        .onClick(() => { // 点击后下滑100.0距离
          this.scroller.scrollTo({ xOffset: 0, yOffset: this.scroller.currentOffset().yOffset + 100 })
        })
        .margin({ top: 10, left: 20 })
      Button('back top')
        .onClick(() => { // 点击后回到顶部
          this.scroller.scrollEdge(Edge.Top)
        })
        .margin({ top: 60, left: 20 })
      Button('next page')
        .onClick(() => { // 点击后下滑到底部
          this.scroller.scrollPage({ next: true })
        })
        .margin({ top: 110, left: 20 })
    }.width('100%').height('100%').backgroundColor(0xDCDCDC)
  }
}
```

Z
zengyawen 已提交
190
![zh-cn_image_0000001174104386](figures/zh-cn_image_0000001174104386.gif)