2

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

如何在背景图像上达到相同的效果?

4

1 回答 1

0

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

于 2017-11-30T17:56:51.483 回答