css - Chrome 鼠标事件不适用于后台的 webkit-transform 元素

标签 css google-chrome mouseevent

我有两个 div。 Page1 有一个按钮...如果你将鼠标悬停在它上面,它会改变它的颜色。

如果我使用 webkit-transform 在后台添加 page2 div,按钮将不会再改变它的颜色。

HTML

<div class="page1">
    <button>hover</button>
</div>
<div class="page2"></div>

CSS

.page1 {
    width: 300px;
    height: 300px;
    border: 1px solid #333;
    position: relative;
    background: #FFF;
    z-index: 2;
}
.page2 {
    width: 300px;
    height: 300px;
    background: #999;
    border: 1px solid #333;
    -webkit-transform: matrix3d(0.78784, 0, 0.13891, -0.00034, 0, 0.8, 0, 0, -0.17364, 0, 0.9848, -0.00246, 0, 0, 0, 1);
    position: absolute;
    top: 0;
    left: 50px;
    z-index: 1;
}
button {
    position: absolute;
    top: 50%;
    right: 1px;
    background: green;
    border: 0;
    font-size: 18px;
    color: #FFF;
}
button:hover {
    background: red;
}

jsfiddle

最佳答案

正如您已经说过的,由于 transform 属性,元素 .page2 被渲染在另一个元素的前面。

enter image description here

解决此问题的一种方法是添加 pointer-events: none到该元素,以防止它影响其他元素。

.page2 {
    pointer-events: none;
}

MDN states :

the value 'none' instructs the mouse event to go "through" the element and target whatever is "underneath" that element instead.

UPDATED EXAMPLE - 只需注意pointer-eventssupport .

pointer-events 的替代方案是使用 translateZ 将元素向前移动。

EXAMPLE HERE

button {
    -webkit-transform: translateZ(20px);
}

关于css - Chrome 鼠标事件不适用于后台的 webkit-transform 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22339174/

相关文章:

JavaScript 如何将我的 volumeSlider 设置为最新值?

android - 更改 Nativescript Listview `onItemTap` 背景颜色?

html - 用 div 标签替换 html 表格

javascript - 历史记录 replaceState 不再在 Chrome 中用于本地文件

jquery - 如何让 Chrome 记住 AJAX 表单的密码?

windows - SetCursorPos 无法通过远程桌面工作(从 WM_INPUT 事件中读取)

html - 将子子菜单添加到水平菜单

python - 为 Windows 构建 Chromium

javascript - 替换 CSS :hover with jQuery

c - 在 OpenGL 中忽略 macOS 上的模拟鼠标事件