HarmonyOS 鸿蒙Next中页面类似弹框样式跳转H5

发布于 1周前 作者 phonegap100 来自 鸿蒙OS

HarmonyOS 鸿蒙Next中页面类似弹框样式跳转H5 页面类似弹框样式跳转H5
现在有一个场景,从A页面跳转到B页面,B页面其实是个H5,且不是全屏,需要看到A页面的内容,相当于Dialog。解释一下,这里为什么不用CustomDialog,因为按照文档说明,CustomDialog需要绑定页面,而这里的场景,这个登录是第三方的库,用CustomDialog对接入方来说不便捷

3 回复

可以使用subWindow的方式弹窗,参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V13/application-window-stage-V13#设置应用子窗口

示例代码:

@Entry
@Component
struct Index {
  webController: webview.WebviewController = new webview.WebviewController();
  aboutToAppear(): void {
    webview.WebviewController.setWebDebuggingAccess(true)
    const windowStage: window.WindowStage | undefined = AppStorage.get("windowStage")
    if (windowStage) {
      windowStage.createSubWindow('test', (err, win) => {
        if (err.code) {
          console.log(JSON.stringify(err.code))
        } else {
          win.loadContentByName(entryName);
          win.resize(500, 500);
          win.moveWindowTo(500, 500)
          win.showWindow();
        }
      })
    }
  }

  build() {
    Row() {
      Column() {
        Text('Index')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
    .width('100%')
}
.height('100%')
  }
}

Page3:

export const entryName: string = 'Page3';
@Entry({ routeName: entryName })
@Component
struct Page3 {
  @State message: string = 'Hello World';
  webController: webview.WebviewController = new webview.WebviewController();

  build() {
    Row() {
      Column() {
        Web({ src:'https://m.baidu.com',controller:this.webController })
          .domStorageAccess(true)
          .onTitleReceive(event => {
            if (event?.title) {
              console.log('receive title: ----- ' + event.title)
            } else {
              console.log('receive title not found ')
            }
          })
      }
      .width('100%')
    }
    .height('100%')
    .backgroundColor(Color.Red)
  }
}

更多关于HarmonyOS 鸿蒙Next中页面类似弹框样式跳转H5的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,页面类似弹框样式跳转H5可以通过使用WebView组件和Dialog组件来实现。首先,创建一个WebView组件用于加载H5页面。然后,使用Dialog组件来模拟弹框样式。通过设置Dialog的布局和样式,将WebView嵌入到Dialog中,从而实现H5页面的弹框跳转效果。具体实现如下:

  1. 在布局文件中定义WebViewDialog的样式。
  2. 在代码中初始化WebView,并加载指定的H5页面URL。
  3. 创建Dialog实例,并将WebView添加到Dialog的布局中。
  4. 控制Dialog的显示和隐藏,实现弹框效果。

代码示例:

import webview from '@ohos.web.webview';
import prompt from '@ohos.prompt';

let webView = webview.WebView.createInstance();
webView.loadUrl('https://example.com');

let dialog = new prompt.Dialog();
dialog.setCustomView(webView);
dialog.show();

在HarmonyOS鸿蒙Next中,若需实现类似弹框样式的页面跳转至H5页面,可以使用WebView组件嵌入H5内容,并通过CustomDialogControllerPageTransition实现弹框效果。具体步骤如下:

  1. 创建WebView组件:在布局文件中添加WebView,并加载目标H5页面的URL。
  2. 弹框控制:使用CustomDialogControllerPageTransition控制弹框的显示与隐藏。
  3. 样式调整:通过设置弹框的宽高、背景透明度等属性,优化用户体验。

示例代码片段:

// 创建WebView
const webView = new WebView();
webView.src = 'https://example.com';

// 弹框控制器
const dialogController = new CustomDialogController({
    builder: webView,
    alignment: DialogAlignment.Bottom
});

// 显示弹框
dialogController.show();

通过以上步骤,可以实现类似弹框样式的H5页面跳转,提升应用交互体验。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!