我正在将阴影过滤器应用于根据用户交互显示和隐藏的 div(下拉列表),但是,在 Safari 中,即使元素被隐藏,阴影仍然可见。
该错误仅发生在 Safari 中。
document.querySelector('.bt').addEventListener('click', function () {
let b = document.querySelector('.b');
if (b.style.display === "none") {
b.style.display = "block";
} else {
b.style.display = "none";
}
})Run Code Online (Sandbox Code Playgroud)
.bt {
padding: 10px;
margin-bottom: 40px;
}
.a {
height: 100px;
padding: 20px;
background: #ccc;
}
.b {
position: relative;
width: 50px;
height: 50px;
background: #0f0;
filter: drop-shadow(0 0 0.125rem #000);
}
.b::after {
content: '';
position: absolute;
display: inline-block;
width: 0;
height: 0;
border-right: .5rem solid transparent;
border-left: .5rem solid transparent;
border-bottom: .5rem solid #0f0;
top: -.5rem;
left: .5rem;
}Run Code Online (Sandbox Code Playgroud)
<h1>drop-shadow filter bug with Safari</h1>
<button class="bt">CLICK ME!</button>
<div class="a">
<div class="b">
.b
</div>
</div>Run Code Online (Sandbox Code Playgroud)
小智 10
我想我找到了一个可能的解决方法。
尝试添加will-change: filter;到您的.b { ... }样式中。这个问题似乎是通过这种方式解决的。
这是带有修复程序的代码片段:
document.querySelector('.bt').addEventListener('click', function () {
let b = document.querySelector('.b');
if (b.style.display === "none") {
b.style.display = "block";
} else {
b.style.display = "none";
}
})Run Code Online (Sandbox Code Playgroud)
.bt {
padding: 10px;
margin-bottom: 40px;
}
.a {
height: 100px;
padding: 20px;
background: #ccc;
}
.b {
will-change: filter;
position: relative;
width: 50px;
height: 50px;
background: #0f0;
filter: drop-shadow(0 0 0.125rem #000);
}
.b::after {
content: '';
position: absolute;
display: inline-block;
width: 0;
height: 0;
border-right: .5rem solid transparent;
border-left: .5rem solid transparent;
border-bottom: .5rem solid #0f0;
top: -.5rem;
left: .5rem;
}Run Code Online (Sandbox Code Playgroud)
<h1>drop-shadow filter bug with Safari</h1>
<button class="bt">CLICK ME!</button>
<div class="a">
<div class="b">
.b
</div>
</div>Run Code Online (Sandbox Code Playgroud)