css - 如何将悬停效果定向到过滤器?

标签 css hover css-filters

我正在尝试向图像添加滑动悬停滤镜效果。我原来的过滤器是灰度(1)。

所以我需要过滤器以方向而不是 slider 覆盖效果来转换以使其变为彩色:“灰度(0)”。

基本上我希望过滤器受到影响。

是否可以?

先感谢您。

* {
  box-sizing: border-box;
}

body {
  background: #fefefe;
  color: #333;
  font: 14px/1.5 "Fira Sans", sans-serif;
}

h1 {
  font-size: 2.5rem;
  font-weight: 300;
  margin: 1.5em 0.5rem 1em;
  text-align: center;
}

.container {
  margin: 0 auto;
  padding: 2rem;
  max-width: 1200px;
}

.row {
  display: flex;
}

.col {
  color: #fff;
  flex: 1 1 auto;
  min-height: 260px;
  position: relative;
}

.col h2 {
  font-weight: 300;
  font-size: 1.33333rem;
  line-height: 1.25;
  margin: 0;
  position: absolute;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 0;
}

.col:nth-child(2) {
  min-width: 20%;
}

.col:nth-child(4) {
  min-width: 33%;
}

.col:nth-child(3)+.col:nth-child(3) {
  min-width: 50%;
}

.img-container {
  background: #0f0523 50% 50%/cover;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  transition: 1s;
  transform-origin: bottom right;
  filter: grayscale(1);
}

.img-container::before {
  background: linear-gradient(transparent, rgba(67, 17, 51, 0.5), #000320);
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.col:hover .photo-container {
  transform: scale(1.25);
}

.slide {
  background: rgba(0, 0, 0, 0.4);
  padding: 0 1.5rem;
}


/* THE EFFECT */

.col {
  overflow: hidden;
  position: relative;
}

.slide {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  transition: all 0.275s ease-in-out, visibility 0s 0.275s;
  visibility: hidden;
  will-change: transform;
  transform: translateY(100%);
}

.row:hover~.row .slide {
  transform: translateY(-100%);
}

.row:hover .slide {
  transform: translateX(100%);
}

.row:hover .col:hover~.col .slide {
  transform: translateX(-100%);
}

.row:hover .col:hover .slide {
  transform: none;
  visibility: visible;
  transition-delay: 0s;
}
<div class="container">
  <div class="row">
    <div class="col">
      <div class="img-container" style="background-image:url(https://source.unsplash.com/600x250/?sig=80);"></div>
      <h2>1 </h2>
      <div class="slide"></div>
    </div>
  </div>
  <div class="row">
    <div class="col">
      <div class="img-container" style="background-image:url(https://source.unsplash.com/600x250/?sig=212);"></div>
      <h2>2 </h2>
      <div class="slide"></div>
    </div>
    <div class="col">
      <div class="img-container" style="background-image:url(https://source.unsplash.com/600x250/?sig=242);"></div>
      <h2>3 </h2>
      <div class="slide"></div>
    </div>

最佳答案

这是一个使用 mask 的想法您可以在其中设置 mask-position 的动画.您还可以依靠图像的伪元素来避免额外的元素。诀窍是让两张图像相互叠加,然后我们调整顶部的 mask 以显示底部的图像。

我删除了文本以仅保留与滤镜效果相关的代码:

* {
  box-sizing: border-box;
}

body {
  background: #fefefe;
}

.container {
  margin: 0 auto;
  padding: 2rem;
  max-width: 1200px;
}

.row {
  display: flex;
}

.col {
  color: #fff;
  flex: 1 1 auto;
  min-height: 260px;
  position: relative;
}

.img-container {
  background-size:0 0;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
.img-container::before,
.img-container::after {
  content: "";
  background-image: inherit;
  background-size:cover;
  background-position:center;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;

}
.img-container::before {
  filter: grayscale(1);
}

.img-container::after {
  transition: all 0.275s ease-in-out, visibility 0s 0.275s;
  visibility: hidden;
  -webkit-mask: linear-gradient(#fff,#fff);
  -webkit-mask-size: 200% 200%;
  -webkit-mask-position:left 0 bottom 200%;
  -webkit-mask-repeat: no-repeat;
  mask: linear-gradient(#fff,#fff);
  mask-size: 200% 200%;
  mask-position:left 0 bottom 200%;
  mask-repeat: no-repeat;
}


.row:hover~.row .img-container::after {
  -webkit-mask-position:left 0 top 200%;
  mask-position:left 0 top 200%;
}

.row:hover .img-container::after {
  -webkit-mask-position:right 200% top 0;
  mask-position:right 200% top 0;

}

.row:hover .col:hover~.col .img-container::after {
  -webkit-mask-position:left 200% top 0;
  mask-position:left 200% top 0;
}

.row:hover .col:hover .img-container::after {
  visibility: visible;
  transition-delay: 0s;
  -webkit-mask-position:0 0%;
  mask-position:0 0%;
  
}
<div class="container">
  <div class="row">
    <div class="col">
      <div class="img-container" style="background-image:url(https://picsum.photos/id/1012/800/800);"></div>
    </div>
  </div>
  <div class="row">
    <div class="col">
      <div class="img-container" style="background-image:url(https://picsum.photos/id/1014/800/800);"></div>
    </div>
    <div class="col">
      <div class="img-container" style="background-image:url(https://picsum.photos/id/16/800/800);"></div>
    </div>
  </div>
</div>

mask在这种情况下,工作方式与背景相同,因此您可以查看此问题/答案以获取有关计算的更多详细信息:Using percentage values with background-position on a linear gradient

更改带有背景的蒙版以更好地查看技巧:

* {
  box-sizing: border-box;
}

body {
  background: grey;
}

.container {
  margin: 0 auto;
  padding: 2rem;
  max-width: 1200px;
}

.row {
  display: flex;
}

.col {
  color: #fff;
  flex: 1 1 auto;
  min-height: 260px;
  position: relative;
}


.col:nth-child(2) {
  min-width: 20%;
}

.col:nth-child(4) {
  min-width: 33%;
}

.col:nth-child(3)+.col:nth-child(3) {
  min-width: 50%;
}

.img-container {
  background-position:center;
  background-size:0 0;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  transition: 1s;
  transform-origin: bottom right;
}

.img-container::before {
  background: inherit;
  background-size:cover;
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  filter: grayscale(1);
}

.img-container::after {
  content:"";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  transition: all 0.275s ease-in-out, visibility 0s 0.275s;
  visibility: hidden;
  background: linear-gradient(#fff,#fff);
  background-size: 200% 200%;
  background-position:left 0 bottom 200%;
  background-repeat: no-repeat;
}


.col {
  overflow: hidden;
  position: relative;
}

.row:hover~.row .img-container::after {
  background-position:left 0 top 200%;
}

.row:hover .img-container::after {
  background-position:right 200% top 0;

}

.row:hover .col:hover~.col .img-container::after {
  background-position:left 200% top 0;
}

.row:hover .col:hover .img-container::after {
  visibility: visible;
  transition-delay: 0s;
  background-position:0 0%;
  
}
<div class="container">
  <div class="row">
    <div class="col">
      <div class="img-container" style="background-image:url(https://picsum.photos/id/1012/800/800);"></div>
    </div>
  </div>
  <div class="row">
    <div class="col">
      <div class="img-container" style="background-image:url(https://picsum.photos/id/1014/800/800);"></div>
    </div>
    <div class="col">
      <div class="img-container" style="background-image:url(https://picsum.photos/id/16/800/800);"></div>
    </div>
  </div>
</div>


基本上白色是图像的可见部分,所以滑动它会使我们没有过滤器的图像可见。

关于css - 如何将悬停效果定向到过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59873257/

相关文章:

html - 使用 z-index 在父 div 后面创建子 div?

javascript - 为什么我的代码 (jQuery) 没有响应?目标 : Sticky header

javascript function rotate element => 尝试为传入的特定参数设置 IF 条件

iphone - 网页可以向左拖动并在移动 View 中放大/缩小

html - 图像上的模糊部分、实心边缘、响应式图像

Canvas 上的 CSS 饱和过滤器

javascript - 如何使 HTML 框在悬停时滑动并使用 JS 或 CSS 显示其下方的文本

html - CSS/Less/Sass - 在 :hover 时匹配之前的所有 sibling

css - 控制悬停区域和 CSS 图像 Sprite

css - 如何将 CSS 滤镜应用于背景图像