【问题标题】:Transition on new pseudo-element with filter带有过滤器的新伪元素上的转换
【发布时间】:2022-01-12 01:25:20
【问题描述】:

我正在为图像制作发光效果。 为了使发光不止一种颜色,我实际上是在制作图像的伪元素副本,并应用了模糊和饱和度过滤器。这个伪元素是通过向图像的 div 添加一个类来创建的。

现在这一切都很好,但我想通过缓出来过渡发光效果,但是无论我将过渡属性放在哪里,我似乎都无法让它工作。

我制作了一个代码笔来说明: https://codepen.io/anon/pen/eYGJGPG

function addGlow() {
  document.getElementById('example').classList.toggle('mask')
}
.img {
  height: fit-content;
  width: fit-content;
  display: block;
  /* Doesn't transition */
  transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
}

.go {
  height: 400px;
  width: 400px;
  background: url("https://upload.wikimedia.org/wikipedia/commons/3/32/RGB_color_wheel_72.svg");
  background-size: initial;
}

.mask::after {
  content: "";
  width: inherit;
  height: inherit;
  position: absolute;
  background: inherit;
  background-position: center center;
  filter: blur(10px) saturate(3);
  z-index: -1;
}
<div id="example" class="img go"></div>
<button onclick="addGlow()">Press</button>

【问题讨论】:

    标签: html css css-transitions


    【解决方案1】:

    当您删除类 .mask 时,伪元素 ::after 将从 DOM 中删除。您需要始终在 DOM 中保留 ::after 以查看其上发生的转换:

    function addGlow() {
      document.getElementById('example').classList.toggle('mask')
    }
    .img {
      height: fit-content;
      width: fit-content;
      display: block;
    }
    
    .go {
      height: 400px;
      width: 400px;
      background: url("https://upload.wikimedia.org/wikipedia/commons/3/32/RGB_color_wheel_72.svg");
      background-size: initial;
    }
    
    .img::after {
      content: "";
      width: inherit;
      height: inherit;
      position: absolute;
      background: inherit;
      background-position: center center;
      z-index: -1;
      
      /* initial state */
      filter: blur(0px) saturate(0);
      transition: all 1.3s ease-out;
      -webkit-transition: all 1.3s ease-out;
      -moz-transition: all 1.3s ease-out;
      -o-transition: all 1.3s ease-out;
    }
    
    .mask::after {
      /* glow */
      filter: blur(30px) saturate(3);
    }
    <div id="example" class="img go"></div>
    <button onclick="addGlow()">Press</button>

    【讨论】:

      【解决方案2】:

      可能考虑使用animation 而不是transition。请参阅下面的工作 sn-p。

      function addGlow(){
        document.getElementById('example').classList.toggle('mask')
      }
      .img {
        height: fit-content;
        width: fit-content;
        display: block;
        /* Doesn't transition */
      
      }
      
      .go {
        height: 400px;
        width: 400px;
        background: url("https://upload.wikimedia.org/wikipedia/commons/3/32/RGB_color_wheel_72.svg");
        background-size: initial;
      
      }
      
      
      .mask::after {
        content: "";
        width: inherit;
        height: inherit;
        position: absolute;
        background: inherit;
        background-position: center center;
        z-index: -1;
        animation: example-animation .5s ease-out;
        animation-direction: normal;
        animation-iteration-count: 1;
        filter: blur(10px) saturate(3);
      }
      
      @keyframes example-animation {
        0% {
            filter: blur(0) saturate(3);
      
        }
        100% {
           filter: blur(10px) saturate(3);
      
        }
      }
      <div id="example" class="img go"></div>
      <button onclick="addGlow()">Press</button>

      【讨论】:

      • 没想到直接用动画,谢谢!但是,在您的示例中,由于我们在蒙版上添加动画,因此只有在添加类时才会缓和,而不是在删除时,对吧?我将如何为正在添加和删除的类制作缓入和缓出动画
      • 好问题.....您可以通过将animation-direction: normal; 更改为animation-direction: reverse; 来反转动画,但是如何使用您的JS 代码切换它,我不太确定。如果我能想到,我会更新我的答案。
      猜你喜欢
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 2015-03-21
      相关资源
      最近更新 更多