如何阻止Flex容器鼠标事件穿透?

在 webview 控件上放了一层 flex 容器作为遮罩,但这个遮罩鼠标事件(hover / click 等)都会穿透到后面的 webview,触摸事件则不会,请问应该如何设置处理来阻止鼠标事件穿透?

阅读 386
avatarAI BotBETA

要阻止 Flex 容器鼠标事件穿透,你可以尝试使用 CSS 的 pointer-events 属性。这个属性可以控制元素是否能够接收鼠标事件。你可以将该属性的值设置为 none,以阻止鼠标事件穿透。

下面是一个示例代码:

.flex-container {
  pointer-events: none;
}

在上面的代码中,.flex-container 是你的 Flex 容器的类名或选择器。通过将 pointer-events 属性设置为 none,Flex 容器将不再接收鼠标事件,从而阻止了鼠标事件穿透。

请注意,这种方法只会阻止鼠标事件穿透,而不会影响触摸事件。如果你需要同时阻止鼠标和触摸事件穿透,你可能需要使用其他方法或技术来实现。

1 个回答

可以参考使用此方法解决:.onMouse((event) => { event.stopPropagation() })

MouseEvent的回调函数stopPropagation用于设置当前事件是否阻止冒泡。

示例代码:

Button(this.isHovered ? 'Hovered!' : 'Not Hover') 
  .width(200) 
  .height(100) 
  .backgroundColor(this.isHovered ? Color.Green : Color.Gray) 
  .onHover((isHover: boolean) => { 
    this.isHovered = isHover; 
  }) 
  .onMouse((event: MouseEvent) => { 
    event.stopPropagation(); // 在Button的onMouse事件中设置阻止冒泡 
    this.buttonText = 'Button onMouse:\n' + '' + 
      'button = ' + event.button + '\n' + 
      'action = ' + event.action + '\n' + 
      'x,y = (' + event.x + ',' + event.y + ')' + '\n' + 
      'screenXY=(' + event.screenX + ',' + event.screenY + ')'; 
  })

参考链接:
stopPropagation

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进