<img> 和混合混合模式的背景图像之间的区别
Difference between <img> and background-image with 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 颜色和背景混合模式,但似乎根本不起作用。
如何在背景图片上实现相同的效果?
您可以做的一件事就是将 <img />
替换为 <div style="background-image: url(...)"></div>
并相应地调整样式(本质上将 img
替换为 div
- 同时给出 div
尺寸 width: 100%; height: 100%;
)。这样它 "layered" 是一样的。
因此您的标记将是:
<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
我正在尝试创建类似于这支笔上显示的双色调效果: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 颜色和背景混合模式,但似乎根本不起作用。
如何在背景图片上实现相同的效果?
您可以做的一件事就是将 <img />
替换为 <div style="background-image: url(...)"></div>
并相应地调整样式(本质上将 img
替换为 div
- 同时给出 div
尺寸 width: 100%; height: 100%;
)。这样它 "layered" 是一样的。
因此您的标记将是:
<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