HarmonyOS Next开发学习手册——Web组件嵌套滚动

Web组件嵌套滚动的典型应用场景为,在一个页面中,有多个独立的区域需要进行滚动,当用户滚动Web区域内容时,可带动其他滚动区域进行滚动,以达到上下滑动页面的用户体验。

内嵌在可滚动容器( Scroll 、 List …)中的Web组件,接收到滑动手势事件,需要对接ArkUI框架的 NestedScrollMode 枚举类型,使得Web组件可以嵌套ArkUI可滚动容器,进行嵌套滚动。开发者可以在Web组件创建时,使用 nestedScroll 属性接口指定默认的嵌套滚动模式,也允许在过程中动态改变嵌套滚动的模式。

nestedScroll入参为一个 NestedScrollOptions 对象,该对象具有两个属性,分别为scrollForward和scrollBackward,每一个属性都为一个 NestedScrollMode 枚举类型。

当Web组件被多个可滚动容器组件嵌套时,未被Web组件消费的与父组件方向一致的偏移量、速度值将被传递给距Web组件最近且方向一致的父组件,使得父组件可以继续滚动。一次手势滑动只能沿X轴或Y轴一个方向嵌套滚动,当手势斜向滑动时,滚动方向为偏移量或速度在X轴、Y轴绝对值较大的方向;当偏移量或速度绝对值在X轴、Y轴绝对值相同时,滚动方向为距Web组件最近的可滚动组件的方向。

说明

  • 支持嵌套滚动的容器:Grid、List、Scroll、Swiper、Tabs、WaterFlow。
  • 支持嵌套滚动的输入事件:使用手势、鼠标、触控板。
// xxx.ets
import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct NestedScroll {
  private scrollerForScroll: Scroller = new Scroller();
  controller: webview.WebviewController = new webview.WebviewController();
  controller2: webview.WebviewController = new webview.WebviewController();
  // NestedScrollMode设置成SELF_ONLY时,Web网页滚动到页面边缘后,不与父组件联动,父组件仍无法滚动。
  @State NestedScrollMode0: NestedScrollMode = NestedScrollMode.SELF_ONLY;
  // NestedScrollMode设置成SELF_FIRST时,Web网页滚动到页面边缘后,父组件继续滚动。
  @State NestedScrollMode1: NestedScrollMode = NestedScrollMode.SELF_FIRST;
  // NestedScrollMode设置为PARENT_FIRST时,父组件先滚动,滚动至边缘后通知Web继续滚动。
  @State NestedScrollMode2: NestedScrollMode = NestedScrollMode.PARENT_FIRST;
  // NestedScrollMode设置为PARALLEL时,父组件与Web同时滚动。
  @State NestedScrollMode3: NestedScrollMode = NestedScrollMode.PARALLEL;
  @State NestedScrollModeF: NestedScrollMode = NestedScrollMode.SELF_FIRST;
  @State NestedScrollModeB: NestedScrollMode = NestedScrollMode.SELF_FIRST;
  // scroll竖向的滚动
  @State ScrollDirection: ScrollDirection = ScrollDirection.Vertical;

  build() {
    Flex() {
      Scroll(this.scrollerForScroll) {
        Column({ space: 5 }) {
          R
HarmonyOS NEXT 中,使用 Web 组件加载本地 HTML 页面是一种常见的需求,适用于展示本地资源或静态内容。以下是几种实现方式及相关代码示例。 ### 3.1 使用 `$rawfile` 加载本地 HTML 文件 最直接的方法是将 HTML 文件放置在 `resources/rawfile` 目录下,并通过 `$rawfile()` 函数引用该文件路径,然后将其作为 `Web` 组件的 `src` 属性值。 ```typescript import webview from '@ohos.web.webview'; @Entry @Component struct WebPage { private controller: webview.WebviewController = new webview.WebviewController(); build() { Column() { // 加载位于 resources/rawfile 下的 index.html 文件 Web({ src: $rawfile('index.html'), controller: this.controller }) } } } ``` 上述代码中,`$rawfile('index.html')` 表示从 `resources/rawfile` 目录加载名为 `index.html` 的文件[^3]。 ### 3.2 动态加载 HTML 字符串(Data URL) 如果希望不依赖外部文件,而是直接在代码中嵌入 HTML 内容,可以使用 Data URL 的方式。这种方式适合加载小型 HTML 内容。 ```typescript import { webview } from '@kit.ArkWeb'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct WebComponent { controller: webview.WebviewController = new webview.WebviewController(); htmlStr: string = "data:text/html,<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>"; build() { Column() { Web({ src: this.htmlStr, controller: this.controller }) } } } ``` 此方法通过字符串构造 HTML 内容并以 Data URL 形式传递给 Web 组件,适用于动态生成的页面内容[^2]。 ### 3.3 结合按钮事件切换本地页面 还可以结合用户交互操作来加载不同的本地 HTML 页面,例如点击按钮切换到另一个 HTML 页面。 ```typescript import { webview } from '@kit.ArkWeb'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct WebComponent { controller: webview.WebviewController = new webview.WebviewController(); build() { Column() { Button('Load local1.html') .onClick(() => { try { // 点击按钮时加载 local1.html this.controller.loadUrl($rawfile("local1.html")); } catch (error) { console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); } }) // 初始加载 local.html Web({ src: $rawfile("local.html"), controller: this.controller }) } } } ``` 在此示例中,初始加载的是 `local.html`,点击按钮后会加载 `local1.html`。这种方式允许用户通过界面操作控制 Web 组件加载不同页面[^4]。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值