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

Z
zengyawen 已提交
3

4
The **\<Scroll>** component scrolls the content when the layout size of a component exceeds the viewport of its parent component.
Z
zengyawen 已提交
5

6 7 8
> **NOTE**
>
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
9 10 11


## Required Permissions
Z
zengyawen 已提交
12 13 14

None

Z
zengyawen 已提交
15 16

## Child Components
Z
zengyawen 已提交
17 18 19

This component supports only one child component.

Z
zengyawen 已提交
20 21 22 23 24 25 26 27

## APIs

Scroll(scroller?: Scroller)


## Attributes

28
| Name       | Type      | Default Value      | Description |
Z
zengyawen 已提交
29
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
30
| scrollable     | ScrollDirection   | ScrollDirection.Vertical | Scroll method. |
31 32
| scrollBar      | [BarState](ts-appendix-enums.md#barstate-enums) | ScrollDirection.Auto | Scrollbar status. |
| scrollBarColor | Color | - | Color of the scrollbar. |
E
esterzhou 已提交
33
| scrollBarWidth | Length | - | Width of the scrollbar. |
Z
zengyawen 已提交
34 35

- ScrollDirection
36
  | Name       | Description |
Z
zengyawen 已提交
37
  | -------- | -------- |
E
ester.zhou 已提交
38 39 40
  | Horizontal | Only horizontal scrolling is supported. |
  | Vertical | Only vertical scrolling is supported. |
  | None     | Scrolling is disabled. |
Z
zengyawen 已提交
41

42 43 44 45 46 47 48 49 50 51 52 53
## Events

| Name | Description |
| -------- | -------- |
| onScrollBegin<sup>9+</sup>(dx: number, dy: number)&nbsp;=&gt;&nbsp;{ dxRemain: number, dyRemain: number } | Called when scrolling starts.<br>Parameters<br>- **dx**: amount to scroll by in the horizontal direction.<br>- **dy**: amount to scroll by in the vertical direction.<br>Return value:<br>- **dxRemain**: remaining amount to scroll by in the horizontal direction.<br>- **dyRemain**: remaining amount to scroll by in the vertical direction.|
| onScroll(xOffset: number, yOffset: number) =&gt; void | Invoked when scrolling starts. It returns the horizontal and vertical offsets. |
| onScrollEdge(side: Edge) =&gt; void | Invoked when scrolling reaches the edge. |
| onScrollEnd() =&gt; void | Invoked when scrolling stops. |

>  **NOTE**
>
>  If the **onScrollBegin** event and **scrollBy** method are used to implement nested scrolling, you must set **edgeEffect** of the child scrolling node to **None**. For example, if the **\<Scroll>** component is nested with the **\<List>** component, the **edgeEffect** attribute of the **\<List>** component must be set to **EdgeEffect.None**.
Z
zengyawen 已提交
54 55 56

## Scroller

57
Controller of the scrollable container component. You can bind this component to the container component and use it to control the scrolling of the container component. Currently, this component can be bound to the **\<List>** and **\<Scroll>** components.
Z
zengyawen 已提交
58 59 60 61


### Objects to Import

Z
zengyawen 已提交
62 63 64 65 66 67

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


68
### scrollTo
Z
zengyawen 已提交
69 70 71

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

Z
zengyawen 已提交
72 73 74

Scrolls to the specified position.

Z
zengyawen 已提交
75 76

- Parameters
E
ester.zhou 已提交
77
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
78
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
79 80
  | xOffset | Length | Yes | - | Horizontal scrolling offset. |
  | yOffset | Length | Yes | - | Vertical scrolling offset. |
81
  | animation | {<br>duration: number,<br>curve: Curve \|<br>CubicBezier \|<br>SpringCurve<br>} | No |  | Animation configuration, which includes the following:<br>- **duration**: scrolling duration.<br>- **curve**: scrolling curve. |
Z
zengyawen 已提交
82 83


84
### scrollEdge
Z
zengyawen 已提交
85 86 87

scrollEdge(value: Edge): void

Z
zengyawen 已提交
88 89 90

Scrolls to the edge of the container.

Z
zengyawen 已提交
91 92

- Parameters
93
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
94
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
95
  | value | Edge | Yes | - | Edge position to scroll to. |
Z
zengyawen 已提交
96 97


98
### scrollPage
Z
zengyawen 已提交
99 100

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

Scrolls to the next or previous page.
Z
zengyawen 已提交
103

Z
zengyawen 已提交
104
- Parameters
105
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
106
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
107
  | next | boolean | Yes | - | Whether to turn to the next page. The value **true** means to scroll to the next page, and the value **false** means to scroll to the previous page. |
108
  | direction | Axis    | No   | -    | Scrolling direction: horizontal or vertical.               |
Z
zengyawen 已提交
109 110


111
### currentOffset
Z
zengyawen 已提交
112 113 114

scroller.currentOffset(): Object

Z
zengyawen 已提交
115 116 117 118

Obtains the scrolling offset.


E
ester.zhou 已提交
119
- Return value
120
    | Type | Description |
Z
zengyawen 已提交
121
  | -------- | -------- |
122
  | {<br>xOffset: number,<br>yOffset: number<br>} | **xOffset**: horizontal scrolling offset.<br>**yOffset**: vertical scrolling offset. |
Z
zengyawen 已提交
123

Z
zengyawen 已提交
124

Z
zengyawen 已提交
125
### scroller.scrollToIndex
Z
zengyawen 已提交
126

Z
zengyawen 已提交
127
scroller.scrollToIndex(value: number): void
Z
zengyawen 已提交
128 129


Z
zengyawen 已提交
130
Scrolls to the specified index.
Z
zengyawen 已提交
131

Z
zengyawen 已提交
132

133 134 135
> **NOTE**
>
> Only the **\<List>** component is supported.
Z
zengyawen 已提交
136 137 138


- Parameters
139
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
140
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
141
  | value | number | Yes | - | Index of the item to be scrolled to in the list. |
Z
zengyawen 已提交
142 143


144
### scrollBy
Z
zengyawen 已提交
145

146
scrollBy(dx: Length, dy: Length): void
Z
zengyawen 已提交
147 148


149
Scrolls by the specified amount.
Z
zengyawen 已提交
150

Z
zengyawen 已提交
151

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
>  **NOTE**
>
>  Only the **\<Scroll>** component is supported.


- Parameters
  | Name  | Type  | Mandatory  | Default Value | Description             |
  | ----- | ------ | ---- | ---- | ----------------- |
  | dx | Length | Yes   | -    | Amount to scroll by in the horizontal direction.|
  | dy | Length | Yes   | -    | Amount to scroll by in the vertical direction.|


## Example

```ts
// xxx.ets
Z
zengyawen 已提交
168 169 170 171 172 173 174 175 176 177 178 179
@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())
180 181 182 183 184 185
              .width('90%')
              .height(150)
              .backgroundColor(0xFFFFFF)
              .borderRadius(15)
              .fontSize(16)
              .textAlign(TextAlign.Center)
Z
zengyawen 已提交
186 187 188 189
              .margin({ top: 10 })
          }, item => item)
        }.width('100%')
      }
190 191 192 193
      .scrollable(ScrollDirection.Vertical)
      .scrollBar(BarState.On)
      .scrollBarColor(Color.Gray)
      .scrollBarWidth(30)
Z
zengyawen 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
      .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(() => {// Click to scroll down 100.0.
          this.scroller.scrollTo({ xOffset: 0, yOffset: this.scroller.currentOffset().yOffset + 100 })
        })
        .margin({ top: 10, left: 20 })
      Button('back top')
        .onClick(() => {// Click to go back to the top.
          this.scroller.scrollEdge(Edge.Top)
        })
        .margin({ top: 60, left: 20 })
      Button('next page')
        .onClick(() => {// Click to scroll down to the bottom.
          this.scroller.scrollPage({ next: true })
        })
        .margin({ top: 110, left: 20 })
    }.width('100%').height('100%').backgroundColor(0xDCDCDC)
  }
}
```

Z
zengyawen 已提交
224
![en-us_image_0000001256978363](figures/en-us_image_0000001256978363.gif)
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
```ts
@Entry
@Component
struct NestedScroll {
  @State listPosition: number = 0 // 0 indicates scrolling to the top of the list, 1 indicates scrolling to the center of the list, and 2 indicates scrolling to the bottom of the list.
  private arr: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  private scroller: Scroller = new Scroller()

  build() {
    Flex() {
      Scroll(this.scroller) {
        Column() {
          Text("Scroll Area")
            .width("100%").height("40%").backgroundColor(0X330000FF)
            .fontSize(16).textAlign(TextAlign.Center)

          List({ space: 20 }) {
            ForEach(this.arr, (item) => {
              ListItem() {
                Text("ListItem" + item)
                  .width("100%").height("100%").borderRadius(15)
                  .fontSize(16).textAlign(TextAlign.Center).backgroundColor(Color.White)
              }.width("100%").height(100)
            }, item => item)
          }
          .width("100%").height("50%").edgeEffect(EdgeEffect.None)
          .onReachStart(() => {
            this.listPosition = 0
          })
          .onReachEnd(() => {
            this.listPosition = 2
          })
          .onScrollBegin((dx: number, dy: number) => {
            if ((this.listPosition == 0 && dy >= 0) || (this.listPosition == 2 && dy <= 0)) {
              this.scroller.scrollBy(0, -dy)
              return { dxRemain: dx, dyRemain: 0 }
            }
            this.listPosition = 1;
            return { dxRemain: dx, dyRemain: dy }
          })

          Text("Scroll Area")
            .width("100%").height("40%").backgroundColor(0X330000FF)
            .fontSize(16).textAlign(TextAlign.Center)
        }
      }
      .width("100%").height("100%")
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding(20)
  }
}
```

![NestedScroll](figures/NestedScroll.gif)