• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 组件可见区域变化事件
2
3组件可见区域变化事件是组件在屏幕中的显示区域面积变化时触发的事件,提供了判断组件是否完全或部分显示在屏幕中的能力,适用于广告曝光埋点之类的场景。
4
5> **说明:**
6>
7>  从API Version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9## onVisibleAreaChange
10
11onVisibleAreaChange(ratios: Array<number>, event: (isVisible: boolean, currentRatio: number) => void): T
12
13组件可见区域变化时触发该回调。
14
15**系统能力:** SystemCapability.ArkUI.ArkUI.Full
16
17**参数:**
18
19| 参数名 | 类型                                                | 必填 | 说明                                                         |
20| ------ | --------------------------------------------------- | ---- | ------------------------------------------------------------ |
21| ratios | Array<number>                                 | 是   | 阈值数组。其中,每个阈值代表组件可见面积(即组件在屏幕显示区的面积,只计算父组件内的面积,超出父组件部分不会计算)与组件自身面积的比值。当组件可见面积与自身面积的比值接近阈值时,均会触发该回调。每个阈值的取值范围为[0.0, 1.0],如果开发者设置的阈值超出该范围,则会实际取值0.0或1.0。 |
22| event  | (isVisible: boolean, currentRatio: number) => void) | 是   | -isVisible:表示组件的可见面积与自身面积的比值与上一次变化相比的情况,比值变大为true,比值变小为false。<br/>-currentRatio:触发回调时,组件可见面积与自身面积的比值。 |
23
24**返回值:**
25
26| 类型 | 说明 |
27| -------- | -------- |
28| T | 返回当前组件。 |
29
30> **说明:**
31>
32> 该接口只适用于组件布局区域超出或离开了当前屏幕显示区域的情况,不支持组件堆叠(Stack)导致的面积不可见、使用offset或translate等图形变换接口导致的面积超出情况。
33
34
35## 示例
36
37```ts
38// xxx.ets
39@Entry
40@Component
41struct ScrollExample {
42  scroller: Scroller = new Scroller()
43  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
44  @State testTextStr: string = 'test'
45  @State testRowStr: string = 'test'
46
47  build() {
48    Column() {
49      Column() {
50        Text(this.testTextStr)
51          .fontSize(20)
52
53        Text(this.testRowStr)
54          .fontSize(20)
55      }
56      .height(100)
57      .backgroundColor(Color.Gray)
58      .opacity(0.3)
59
60      Scroll(this.scroller) {
61        Column() {
62          Text("Test Text Visible Change")
63            .fontSize(20)
64            .height(200)
65            .margin({ top: 50, bottom: 20 })
66            .backgroundColor(Color.Green)
67              // 通过设置ratios为[0.0, 1.0],实现当组件完全显示或完全消失在屏幕中时触发回调
68            .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
69              console.info('Test Text isVisible: ' + isVisible + ', currentRatio:' + currentRatio)
70              if (isVisible && currentRatio >= 1.0) {
71                console.info('Test Text is fully visible. currentRatio:' + currentRatio)
72                this.testTextStr = 'Test Text is fully visible'
73              }
74
75              if (!isVisible && currentRatio <= 0.0) {
76                console.info('Test Text is completely invisible.')
77                this.testTextStr = 'Test Text is completely invisible'
78              }
79            })
80
81          Row() {
82            Text('Test Row Visible  Change')
83              .fontSize(20)
84              .margin({ bottom: 20 })
85
86          }
87          .height(200)
88          .backgroundColor(Color.Yellow)
89          .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
90            console.info('Test Row isVisible:' + isVisible + ', currentRatio:' + currentRatio)
91            if (isVisible && currentRatio >= 1.0) {
92              console.info('Test Row is fully visible.')
93              this.testRowStr = 'Test Row is fully visible'
94            }
95
96            if (!isVisible && currentRatio <= 0.0) {
97              console.info('Test Row is completely invisible.')
98              this.testRowStr = 'Test Row is completely invisible'
99            }
100          })
101
102          ForEach(this.arr, (item:number) => {
103            Text(item.toString())
104              .width('90%')
105              .height(150)
106              .backgroundColor(0xFFFFFF)
107              .borderRadius(15)
108              .fontSize(16)
109              .textAlign(TextAlign.Center)
110              .margin({ top: 10 })
111          }, (item:number) => (item.toString()))
112
113        }.width('100%')
114      }
115      .backgroundColor(0x317aff)
116      .scrollable(ScrollDirection.Vertical)
117      .scrollBar(BarState.On)
118      .scrollBarColor(Color.Gray)
119      .scrollBarWidth(10)
120      .onScroll((xOffset: number, yOffset: number) => {
121        console.info(xOffset + ' ' + yOffset)
122      })
123      .onScrollEdge((side: Edge) => {
124        console.info('To the edge')
125      })
126      .onScrollStop(() => {
127        console.info('Scroll Stop')
128      })
129
130    }.width('100%').height('100%').backgroundColor(0xDCDCDC)
131  }
132}
133```
134
135![zh-cn_visible_area_change.gif](figures/zh-cn_visible_area_change.gif)
136