Safari:即使隐藏元素,阴影过滤器仍然可见

时间:2019-06-06 13:44:22

标签: html css safari

我正在将阴影过滤器应用于根据用户交互显示和隐藏的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";
    }
})
.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;
}
<h1>drop-shadow filter bug with Safari</h1>

<button class="bt">CLICK ME!</button>

<div class="a">
    <div class="b">
        .b
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

我认为我找到了一种可能的解决方法。

尝试将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";
    }
})
.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;
}
<h1>drop-shadow filter bug with Safari</h1>

<button class="bt">CLICK ME!</button>

<div class="a">
    <div class="b">
        .b
    </div>
</div>