HarmonyOS鸿蒙Next中scroll嵌套web首次无法滚动

HarmonyOS鸿蒙Next中scroll嵌套web首次无法滚动 首次无法滚动,必须手指触摸一次后   才可以正常滚动

3 回复

scroll嵌套web 滑动参考示例

import geoLocationManager from '@ohos.geoLocationManager';
import BusinessError from "@ohos.base";
import web_webview from '@ohos.web.webview'
import business_error from '@ohos.base';

@Entry
@Component
struct Index {
  geocodeRequest: geoLocationManager.GeoCodeRequest = { "description": "常州市万达广场", "maxItems": 10 }
  reverseGeoCodeRequest: geoLocationManager.ReverseGeoCodeRequest = {
    latitude: 31.734816113942514,
    longitude: 119.94373220220504,
    locale: "zh",
    maxItems: 10
  }
  controller: web_webview.WebviewController = new web_webview.WebviewController()

  build() {
    Scroll() {
      NavDestination() {
        Stack({ alignContent: Alignment.Top }) {
          Flex() {
            Image($r('app.media.foreground'))
              .height(32)
              .width(32)
              .objectFit(ImageFit.Contain)
              .margin({ left: 8 })
              .onClick(() => {
                // RouterTypeHandler.pop()
              })
            Stack({ alignContent: Alignment.End }) {
              Image($r('app.media.foreground'))
                .width(35)
                .height(35)
                .objectFit(ImageFit.Contain)
                .onClick(() => {
                })
            }
            .height('100%')
            .layoutWeight(1)
          }
          .height(48)
          .width('100%')
          .backgroundColor(Color.Transparent)
          .zIndex(2)
          Column() {
            Image($r('app.media.startIcon'))
              .height(220)
              .objectFit(ImageFit.Cover)
            Text('title')
              .fontColor(Color.Black)
              .fontSize(24)
              .fontWeight(FontWeight.Bold)
              .margin({ top: 18 })
              .padding({
                left: 15,
                top: 10,
                right: 15,
                bottom: 10
              })
            RelativeContainer() {
              Image($r('app.media.startIcon'))
                .id('iv_head')
                .width(36)
                .height(36)
                .borderRadius(50)
              Image($r('app.media.startIcon'))
                .id('iv_office_v')
                .width(15)
                .height(15)
              Text('name')
                .id('name')
                .fontSize(14)
                .fontWeight(FontWeight.Bold)
                .margin({ left: 8 })
              Image($r('app.media.startIcon'))
                .id('iv_flag1')
                .height(2000)
                .width(30)
                .margin({ left: 5 })
              Image($r('app.media.startIcon'))
                .id('iv_flag2')
                .height(14)
                .width(30)
                .margin({ left: 4 })
              Text('description')
                .id('description')
                .fontSize(10)
                .fontColor(Color.Red)
                .margin({ left: 8, top: 4 })
            }
            .height(56)
            .width(100)
            .padding({ left: 20, right: 20 })
            Column() {
              Web({
                src: 'www.example.com',
                controller: this.controller,
                renderMode: RenderMode.SYNC_RENDER
              })
              .javaScriptAccess(true)
              .databaseAccess(true)
              .domStorageAccess(true)
            }
            .height(200)
            .width('100%')
          }
        }
        .alignItems(HorizontalAlign.Start)
        .width('100%')
        .zIndex(1)
      }
    }
    .width('100%')
    .height('500%')
    .padding({ top: AppStorage.get<number>('statusBarHeight') })
    .hideTitleBar(true)
  }
  .scrollable(ScrollDirection.Vertical)  // 滚动方向为垂直方向
  .scrollBar(BarState.On)  // 滚动条常驻显示
  .scrollBarColor(Color.Gray)  // 滚动条颜色
  .scrollBarWidth(10)  // 滚动条宽度
  .edgeEffect(EdgeEffect.Spring)
}

更多关于HarmonyOS鸿蒙Next中scroll嵌套web首次无法滚动的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,遇到scroll嵌套web首次无法滚动的问题,可能是由于布局或事件处理机制导致的。具体原因可能包括以下几点:

  1. 布局层级问题:scroll和web组件的层级关系可能导致事件传递或处理异常。需要检查布局层级,确保scroll能够正确捕获到滚动事件。

  2. 初始化时机问题:web组件在首次加载时可能尚未完全初始化,导致scroll无法正确响应滚动事件。可以尝试在web组件加载完成后手动触发scroll的初始化。

  3. 事件冲突:scroll和web组件可能存在事件冲突,导致滚动事件无法正确传递。需要检查事件处理逻辑,确保两者之间的事件传递不会互相干扰。

  4. 系统版本兼容性:不同版本的HarmonyOS可能存在兼容性问题,导致scroll嵌套web时出现异常。需要确认系统版本,并根据版本特性进行适配。

  5. 组件属性设置:scroll和web组件的某些属性设置可能影响滚动行为。需要检查相关属性,确保其设置符合预期。

解决该问题需要具体分析代码和布局,定位问题原因后进行相应的调整。

在HarmonyOS鸿蒙Next中,如果遇到scroll嵌套web首次无法滚动的问题,可以尝试以下解决方法:

  1. 检查布局:确保scroll和web组件的布局设置正确,特别是高度和宽度的定义。

  2. 设置焦点:在web组件加载完成后,手动设置焦点到web组件上,以确保其可以接收滚动事件。

  3. 延迟加载:尝试在web组件加载完成后再进行滚动操作,可以通过设置一个延迟来实现。

  4. 更新SDK:确保使用的HarmonyOS SDK是最新版本,以获取最新的bug修复和性能优化。

通过这些方法,通常可以解决scroll嵌套web首次无法滚动的问题。如果问题仍然存在,建议查阅官方文档或寻求社区支持。

回到顶部