<img/>与具有混合混合模式的背景图像之间的差异

时间:2017-11-30 17:32:42

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

我正在尝试创建类似于此笔上显示的双色调效果: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颜色,但这似乎根本不起作用。

如何在背景图像上实现相同的效果?

1 个答案:

答案 0 :(得分:0)

您可以做的一件事就是用<img />替换<div style="background-image: url(...)"></div>并相应地调整样式(基本上用img替换div - 同时给出{{1}尺寸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> 

查看此更新的CodePen

相关问题