javascript - 如何将事件从具有较高 z-index 的元素传播到不包括点击事件的元素?
问题描述
我有一个组件,其中有标记。Marker 是带有图标的样式化 div。标记有 click、mouseenter 和 mouseleave 事件。当鼠标进入工具提示出现。在标记之上,我可以放置其他元素来覆盖它们。该元素具有更高的 z-index。我仍然希望能够将 (mouseenter, mouseleave) 悬停在较低的 z-index 元素(标记)上,同时防止它们被覆盖时发生单击事件。是否有任何解决方案可以仅通过少数事件或仅将某些事件排除在较高 z-index 元素上的传播中?
解决方案
<!DOCTYPE html>
<style>
#elmHigherZindexID {
width: 100px;
height: 100px;
position: absolute;
background-color: chartreuse;
z-index: 1000;
}
#elmLowerZindexID {
width: 100px;
height: 100px;
position: absolute;
background-color: cornflowerblue
}
</style>
<body>
<div id="elmHigherZindexID">HIGH</div>
<div id="elmLowerZindexID">LOW</div>
</body>
<script>
let highElmRef = document.getElementById('elmHigherZindexID');
let lowElmRef = document.getElementById('elmLowerZindexID');
highElmRef.addEventListener('click', highEventHandler);
highElmRef.addEventListener('mouseenter', highOtherEventHandler);
lowElmRef.addEventListener('mouseenter', lowEventHandler);
function highEventHandler(event) {
event.stopPropagation();
console.log('high', event);
}
function highOtherEventHandler(event) {
event.stopPropagation();
console.log('high', event);
const cusEvent = new MouseEvent('mouseenter', {
view: window,
bubbles: true,
cancelable: true
});
lowElmRef.dispatchEvent(cusEvent);
}
function lowEventHandler(event) {
event.stopPropagation();
console.log('low', event);
}
</script>
</html>
推荐阅读
- python-3.x - 从 openai 健身房中的图像中学习:渲染功能的内存泄漏或屏幕外 glfw 问题
- javascript - 使用 jQuery 在滚动时加载 DOM 以提高性能
- flutter - Flutter/Dart:如何将地图解析为端点的值
- python - 检测变量值或名称中的变化
- r - 更改输入后 Shiny App 的问题未更新
- cypress - 如何在赛普拉斯测试中等待成功响应
- spring-boot - 使用 Kafka REST 代理发布 Avro 消息会引发“将 JSON 转换为 Avro 失败”
- javascript - 如何 QUIET ALL webpack console.log 输出?
- react-native - React native firebase - facebook登录未触发onAuthStateChanged监听器
- reactjs - 将 reactjs 图像调整为窗口大小