【问题标题】:CSS filter greyscale removes pseudo element overlayCSS滤镜灰度去除伪元素覆盖
【发布时间】:2022-01-25 21:51:15
【问题描述】:

我有一个背景图像,在悬停时我想要灰度并覆盖柔和的灯光颜色校正。我将叠加层作为伪元素,在悬停时应该出现在灰度背景图像的顶部。但是,使用规则filter: grayscale(100%) contrast(1); 不会显示伪元素。这是 CSS 错误吗?

HTML

<div class="item-block">
  <div class="bg-wrapper">
      <div class="bg" style="background-image: url('https://res.cloudinary.com/ho5waxsnl/image/upload/c_fill,f_auto,h_540,q_auto,w_960/yl3h7wm8jdcw7zn95jdjg4ll5owh');"></div>
  </div>
</div> 

CSS

.bg-wrapper .bg:after{
  content: '';
  transition: all .3s ease 0s;
  width: 2000px;
  height: 2000px;
  top: calc(50% - 1000px);
  left: calc(50% - 1000px);
  position: absolute;
  opacity: 0;
  -webkit-transition-duration: 3s;
  -o-transition-duration: 3s;
  transition-duration: 3s;
  background-color: #0e79b2;
  mix-blend-mode: lighten;
}

.bg-wrapper .bg{
  background-size: cover;
  background-repeat: no-repeat;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  transition-duration: 3s;
}

.bg-wrapper{
  height: 500px;
  transition: all .3s ease 0s;
  overflow: hidden;
}

.item-block:hover .bg{
  transform: scale(1.1);
  filter: grayscale(100%) contrast(1);
}

.item-block:hover .bg:after{
  opacity: 1;
} 

【问题讨论】:

    标签: html css


    【解决方案1】:

    尝试去除不透明度:

    .bg-wrapper .bg:after{
      content: '';
      transition: all .3s ease 0s;
      width: 2000px;
      height: 2000px;
      top: calc(50% - 1000px);
      left: calc(50% - 1000px);
      position: absolute;
      opacity: 0;
      -webkit-transition-duration: 3s;
      -o-transition-duration: 3s;
      transition-duration: 3s;
      background-color: #0e79b2;
      mix-blend-mode: lighten;
      filter: grayscale(100%) contrast(1);
    }
    
    .bg-wrapper .bg{
      background-size: cover;
      background-repeat: no-repeat;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      width: 100%;
      height: 100%;
      transition-duration: 3s;
    }
    
    .bg-wrapper{
      height: 500px;
      transition: all .3s ease 0s;
      overflow: hidden;
    }
    .bg-wrapper:hover .bg:after{
      opacity: 1;
    }
    <div class="item-block">
      <div class="bg-wrapper">
          <div class="bg" style="background-image: url('https://res.cloudinary.com/ho5waxsnl/image/upload/c_fill,f_auto,h_540,q_auto,w_960/yl3h7wm8jdcw7zn95jdjg4ll5owh');"></div>
      </div>
    </div>

    【讨论】:

    • 嗨,虽然这在您的演示中有效,但您没有灰度 css 过滤器。这也适用于这条规则吗?
    • @archvist 嘿,伙计,现在请检查
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多