1# Visible Area Change Event 2 3The visible area change event of a component refers to the change in the visual portion of the component on the screen. It can be used to determine whether the component is completely or partially displayed on the screen. It is usually applicable to scenarios such as advertisement exposure tracing. 4 5> **NOTE** 6> 7> The APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version. 8 9## onVisibleAreaChange 10 11onVisibleAreaChange(ratios: Array<number>, event: VisibleAreaChangeCallback): T 12 13Called when the visible area of the component changes. 14 15**Atomic service API**: This API can be used in atomic services since API version 11. 16 17**System capability**: SystemCapability.ArkUI.ArkUI.Full 18 19**Parameters** 20 21| Name| Type | Mandatory| Description | 22| ------ | --------------------------------------------------- | ---- | ------------------------------------------------------------ | 23| ratios | Array<number> | Yes | Threshold array. Each threshold represents a ratio of the component's visible area (that is, the area of the component that is visible on screen; only the area within the parent component is counted) to the component's total area. This callback is invoked when the ratio of the component's visible area to its total area is greater than or less than the threshold. The value range of the threshold is [0.0, 1.0]. If the threshold set exceeds this range, the value **0.0** or **1.0** will be used.<br>**NOTE**<br>When the value is close to the boundary 0 or 1, it is rounded off with a round-off error not greater than 0.001. For example, 0.9997 is rounded off to 1.| 24| event | [VisibleAreaChangeCallback](ts-types.md#visibleareachangecallback12) | Yes | Callback for visible area changes of the component.| 25 26**Return value** 27 28| Type| Description| 29| -------- | -------- | 30| T | Current component.| 31 32> **NOTE** 33> 34> 35>- This API only takes into account the relative clipped area ratio of the component with respect to all ancestor nodes (up to the window boundary) and its own area. 36> 37>- It does not support calculations for obstructions caused by sibling components or by sibling components of any ancestors, such as those managed by [Stack](ts-container-stack.md) or [z-order control](ts-universal-attributes-z-order.md). 38> 39>- It does not support visibility change calculations for nodes that are not in the component tree. For example, preloaded nodes or custom nodes mounted using the [overlay](ts-universal-attributes-overlay.md#overlay) capability. 40 41 42## Example 43 44This example demonstrates how to set an **onVisibleAreaChange** event for a component, which triggers the callback when the component is fully displayed or completely hidden. 45 46```ts 47// xxx.ets 48@Entry 49@Component 50struct ScrollExample { 51 scroller: Scroller = new Scroller() 52 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 53 @State testTextStr: string = 'test' 54 @State testRowStr: string = 'test' 55 56 build() { 57 Column() { 58 Column() { 59 Text(this.testTextStr) 60 .fontSize(20) 61 62 Text(this.testRowStr) 63 .fontSize(20) 64 } 65 .height(100) 66 .backgroundColor(Color.Gray) 67 .opacity(0.3) 68 69 Scroll(this.scroller) { 70 Column() { 71 Text("Test Text Visible Change") 72 .fontSize(20) 73 .height(200) 74 .margin({ top: 50, bottom: 20 }) 75 .backgroundColor(Color.Green) 76 // Set ratios to [0.0, 1.0] to invoke the callback when the component is fully visible or invisible on screen. 77 .onVisibleAreaChange([0.0, 1.0], (isExpanding: boolean, currentRatio: number) => { 78 console.info('Test Text isExpanding: ' + isExpanding + ', currentRatio:' + currentRatio) 79 if (isExpanding && currentRatio >= 1.0) { 80 console.info('Test Text is fully visible. currentRatio:' + currentRatio) 81 this.testTextStr = 'Test Text is fully visible' 82 } 83 84 if (!isExpanding && currentRatio <= 0.0) { 85 console.info('Test Text is completely invisible.') 86 this.testTextStr = 'Test Text is completely invisible' 87 } 88 }) 89 90 Row() { 91 Text('Test Row Visible Change') 92 .fontSize(20) 93 .margin({ bottom: 20 }) 94 95 } 96 .height(200) 97 .backgroundColor(Color.Yellow) 98 .onVisibleAreaChange([0.0, 1.0], (isExpanding: boolean, currentRatio: number) => { 99 console.info('Test Row isExpanding:' + isExpanding + ', currentRatio:' + currentRatio) 100 if (isExpanding && currentRatio >= 1.0) { 101 console.info('Test Row is fully visible.') 102 this.testRowStr = 'Test Row is fully visible' 103 } 104 105 if (!isExpanding && currentRatio <= 0.0) { 106 console.info('Test Row is completely invisible.') 107 this.testRowStr = 'Test Row is completely invisible' 108 } 109 }) 110 111 ForEach(this.arr, (item:number) => { 112 Text(item.toString()) 113 .width('90%') 114 .height(150) 115 .backgroundColor(0xFFFFFF) 116 .borderRadius(15) 117 .fontSize(16) 118 .textAlign(TextAlign.Center) 119 .margin({ top: 10 }) 120 }, (item:number) => (item.toString())) 121 122 }.width('100%') 123 } 124 .backgroundColor(0x317aff) 125 .scrollable(ScrollDirection.Vertical) 126 .scrollBar(BarState.On) 127 .scrollBarColor(Color.Gray) 128 .scrollBarWidth(10) 129 .onWillScroll((xOffset: number, yOffset: number, scrollState: ScrollState) => { 130 console.info(xOffset + ' ' + yOffset) 131 }) 132 .onScrollEdge((side: Edge) => { 133 console.info('To the edge') 134 }) 135 .onScrollStop(() => { 136 console.info('Scroll Stop') 137 }) 138 139 }.width('100%').height('100%').backgroundColor(0xDCDCDC) 140 } 141} 142``` 143 144 145