【问题标题】:Difference between <img> and background-image with mix-blend-mode<img> 和带有混合混合模式的背景图像之间的区别
【发布时间】:2018-05-14 17:31:38
【问题描述】:

我正在尝试创建类似于这支笔上显示的双色调效果:https://codepen.io/meowwwls/pen/zoRjdK

我的尝试就在这里,并且完全按照我想要的方式工作,用于普通的 img 标签:https://codepen.io/sunnywz/pen/zPyYYR

$dark_blue: #080c29;
$white_blue: #dbe6ec;

.duotone {

display: inline-block;
position: relative;
vertical-align: top;
width: auto;

&:before,
&:after {
    content: "";
    opacity: 1;
    pointer-events: none;
    position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    transition: 0.5s;
        -webkit-transition: 0.5s;
}

&:before {
    background: $dark_blue;
    mix-blend-mode: color;
    z-index: 1;
}

&:after {
    background: $white_blue;
    mix-blend-mode: color;
    z-index: 2;
}

img {
    filter: grayscale(1) contrast(1) brightness(1);
        -webkit-filter: grayscale(1) contrast(1) brightness(1);
    vertical-align: middle;
}

&:hover {
    &:before,
    &:after {
        opacity: 0;
        transition: 0.5s;
            -webkit-transition: 0.5s;
    }
    img {
        filter: none;
            -webkit-filter: none;
    }
}

}

.duotone-background {

background-size: cover;
display: inline-block;
filter: grayscale(1) contrast(1) brightness(1);
    -webkit-filter: grayscale(1) contrast(1) brightness(1);
height: 386px;
position: relative;
width: 640px;
vertical-align: top;

&:before,
&:after {
    content: "";
    opacity: 1;
    pointer-events: none;
    position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    transition: 0.5s;
        -webkit-transition: 0.5s;
}

&:before {
    background: $dark_blue;
    mix-blend-mode: color;
    z-index: 1;
}

&:after {
    background: $white_blue;
    mix-blend-mode: color;
    z-index: 2;
}

&:hover {
    filter: none;
        -webkit-filter: none;
    &:before,
    &:after {
        opacity: 0;
        transition: 0.5s;
            -webkit-transition: 0.5s;
    }
}

}

但是当我将相同的样式和伪元素应用到具有背景图像的 div 时,它会产生完全不同的效果,正如您在第二张图像中看到的那样。

我尝试在带有背景混合模式的 div 上使用 $dark_blue 颜色,但这似乎根本不起作用。

如何在背景图片上实现同样的效果?

【问题讨论】:

    标签: css background-image mix-blend-mode


    【解决方案1】:

    您可以做的一件事就是将 &lt;img /&gt; 替换为 &lt;div style="background-image: url(...)"&gt;&lt;/div&gt; 并相应地调整样式(本质上将 img 替换为 div - 也提供 div 尺寸 width: 100%; height: 100%; )。这样它的“分层”相同。

    所以你的标记是:

    <div class="duotone-background" >
      <div style="background-image: url('http://dlvec.btmcdev.com/wp-content/uploads/2017/11/Screen-Shot-2017-11-21-at-10.07.07-AM-1024x617.jpg')"></div>
    </div> 
    

    风格:

    .duotone-background {
        display: inline-block;
        height: 386px;
        position: relative;
        width: 640px;
      vertical-align: top;
    
        &:before,
        &:after {
            content: "";
            opacity: 1;
            pointer-events: none;
            position: absolute;
                top: 0;
                right: 0;
                bottom: 0;
                left: 0;
            transition: 0.5s;
                -webkit-transition: 0.5s;
        }
    
        &:before {
            background: $dark_blue;
            mix-blend-mode: color;
            z-index: 1;
        }
    
        &:after {
            background: $white_blue;
            mix-blend-mode: color;
            z-index: 2;
        }
    
        &:hover {
            filter: none;
                -webkit-filter: none;
            &:before,
            &:after {
                opacity: 0;
                transition: 0.5s;
                    -webkit-transition: 0.5s;
            }
    
        div {
          filter: none;
                -webkit-filter: none;
        }
        }
    
      div {
        width: 100%;
        height: 100%;
        background-size: cover;
        filter: grayscale(1) contrast(1) brightness(1);
            -webkit-filter: grayscale(1) contrast(1) brightness(1);
      }
    }
    

    查看此更新CodePen

    【讨论】:

    • 太棒了!不敢相信我没想到。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2018-04-22
    • 2021-07-25
    • 2017-04-29
    • 2020-07-13
    • 2011-07-23
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多