中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
商品详情页,嵌套滚动的时候父容器往下滑的过程中,惯性传不到WebView里面,但是从webView往上滑的时候,惯性能传递到父布局。
微信扫码分享
import web_webview from '@ohos.web.webview'; @Entry @Component export struct WebScrollerDemo { private scrollTouchDown: boolean = false; private webTouchDown: boolean = false; private scrolling: boolean = false; private scroller: Scroller = new Scroller() controller: web_webview.WebviewController = new web_webview.WebviewController(); build() { Scroll(this.scroller) { Column() { Text("Scroll Area") .width("100%") .height(400) .backgroundColor(0X330000FF) .fontSize(16) .textAlign(TextAlign.Center) Web({ controller: this.controller, src: "https://www.huawei.com/s?id=1778113237500106256&wfr=spider&for=pc", }).height("100%").width("100%") .nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.SELF_FIRST }) .onTouch((event: TouchEvent) => { if (event.type == TouchType.Down) { this.webTouchDown = true; } else if (event.type == TouchType.Up) { this.webTouchDown = false; } }) }.width("100%") } .onTouch((event: TouchEvent) => { if (event.type == TouchType.Down) { this.scrollTouchDown = true; } else if (event.type == TouchType.Up) { this.scrollTouchDown = false; } }) .onScrollFrameBegin((offset: number, state: ScrollState) => { let yOffset: number = this.scroller.currentOffset().yOffset if (this.scrolling && offset > 0) { if (yOffset >= 400) { // 400为上面Text组件高度 this.controller.scrollBy(0, offset) return { offsetRemain: 0 } } else if (yOffset + offset > 400) { this.controller.scrollBy(0, yOffset + offset - 400) return { offsetRemain: 400 - yOffset } } } return { offsetRemain: offset } }) .onScrollStart(() => { if (this.scrollTouchDown && !this.webTouchDown) { this.scrolling = true; } }) .onScrollStop(() => { this.scrolling = false; }) .edgeEffect(EdgeEffect.Spring) .backgroundColor('#DCDCDC') .scrollBar(BarState.On) .width('100%') .height('100%') } }