W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
組件可見區(qū)域變化事件是組件在屏幕中的顯示區(qū)域面積變化時(shí)觸發(fā)的事件,提供了判斷組件是否完全或部分顯示在屏幕中的能力,適用于廣告曝光埋點(diǎn)之類的場(chǎng)景。
從API Version 9開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
名稱 | 功能描述 |
---|---|
onVisibleAreaChange(ratios: Array<number>, event: (isVisible: boolean, currentRatio: number) => void) | 組件可見區(qū)域變化時(shí)觸發(fā)該回調(diào)。 -ratios:閾值數(shù)組。其中,每個(gè)閾值代表組件可見面積(即組件在屏幕顯示區(qū)的面積)與組件自身面積的比值。當(dāng)組件可見面積與自身面積的比值大于或小于閾值時(shí),均會(huì)觸發(fā)該回調(diào)。每個(gè)閾值的取值范圍為[0.0, 1.0],如果開發(fā)者設(shè)置的閾值超出該范圍,則會(huì)實(shí)際取值0.0或1.0. -isVisible:表示組件的可見面積與自身面積的比值是否大于閾值,true表示大于,false表示小于。 -currentRatio:觸發(fā)回調(diào)時(shí),組件可見面積與自身面積的比值。 說(shuō)明: 該接口只適用于組件布局區(qū)域超出或離開了當(dāng)前屏幕顯示區(qū)域的情況,不支持組件堆疊(Stack)導(dǎo)致的面積不可見、使用offset或translate等圖形變換接口導(dǎo)致的面積超出情況。 |
- // xxx.ets
- @Entry
- @Component
- struct ScrollExample {
- scroller: Scroller = new Scroller()
- private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- @State testTextStr: string = 'test'
- @State testRowStr: string = 'test'
- build() {
- Column() {
- Column() {
- Text(this.testTextStr)
- .fontSize(20)
- Text(this.testRowStr)
- .fontSize(20)
- }
- .height(100)
- .backgroundColor(Color.Gray)
- .opacity(0.3)
- Scroll(this.scroller) {
- Column() {
- Text("Test Text Visible Change")
- .fontSize(20)
- .height(200)
- .margin({ top: 50, bottom: 20 })
- .backgroundColor(Color.Green)
- // 通過設(shè)置ratios為[0.0, 1.0],實(shí)現(xiàn)當(dāng)組件完全顯示或完全消失在屏幕中時(shí)觸發(fā)回調(diào)
- .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
- console.info('Test Text isVisible: ' + isVisible + ', currentRatio:' + currentRatio)
- if (isVisible && currentRatio >= 1.0) {
- console.info('Test Text is fully visible. currentRatio:' + currentRatio)
- this.testTextStr = 'Test Text is fully visible'
- }
- if (!isVisible && currentRatio <= 0.0) {
- console.info('Test Text is completely invisible.')
- this.testTextStr = 'Test Text is completely invisible'
- }
- })
- Row() {
- Text('Test Row Visible Change')
- .fontSize(20)
- .margin({ bottom: 20 })
- }
- .height(200)
- .backgroundColor(Color.Yellow)
- .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
- console.info('Test Row isVisible:' + isVisible + ', currentRatio:' + currentRatio)
- if (isVisible && currentRatio >= 1.0) {
- console.info('Test Row is fully visible.')
- this.testRowStr = 'Test Row is fully visible'
- }
- if (!isVisible && currentRatio <= 0.0) {
- console.info('Test Row is is completely invisible.')
- this.testRowStr = 'Test Row is is completely invisible'
- }
- })
- 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%')
- }
- .backgroundColor(0x317aff)
- .scrollable(ScrollDirection.Vertical)
- .scrollBar(BarState.On)
- .scrollBarColor(Color.Gray)
- .scrollBarWidth(10)
- .onScroll((xOffset: number, yOffset: number) => {
- console.info(xOffset + ' ' + yOffset)
- })
- .onScrollEdge((side: Edge) => {
- console.info('To the edge')
- })
- .onScrollEnd(() => {
- console.info('Scroll Stop')
- })
- }.width('100%').height('100%').backgroundColor(0xDCDCDC)
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: