简体   繁体   English

将鼠标悬停在 div 上时移除图像灰度效果

[英]Remove image grayscale effect while hovering on a div

I'm making a sidebar and I want it so that when you hover over a specific div the image of that div will change the grayscale from 100% to 0%.我正在制作一个侧边栏,我想要它,以便当您将鼠标悬停在特定 div 上时,该 div 的图像会将灰度从 100% 更改为 0%。 Here's my current progress: https://jsfiddle.net/3j9zLokr/57/这是我目前的进展: https : //jsfiddle.net/3j9zLokr/57/

<div class="example">
 <div class="a">
  <img src="https://sep.yimg.com/ay/filmandvideolighting/times-square-106-primary-red-lighting-gel-sheet-10x10-in-1.jpg" class="one" width="60" />
 </div>
 <div class="b">
  <img src="https://sep.yimg.com/ay/filmandvideolighting/times-square-106-primary-red-lighting-gel-sheet-10x10-in-1.jpg" class="two" width="60" />
 </div>
</div>

<style>

.one, .two {
  -webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */
  filter: grayscale(100%);
  margin-left: 19px;
  padding-top: 10px;
  padding-bottom: 10px;
}

.two {
  padding-top: 10px;
}

.one {
  padding-bottom: 5px;
}

.example {
  width: 100px;
  background-color: black;
  position: absolute;
  top: 0;
  left: 0;
}

.a {
  outline: 1px solid white;
}

</style>

just ad to your css this code只需将此代码添加到您的 css 中

.one:hover {
  -webkit-filter: grayscale(0%); /* Safari 6.0 - 9.0 */
  filter: grayscale(0%);
}

.two:hover {
  -webkit-filter: grayscale(0%); /* Safari 6.0 - 9.0 */
  filter: grayscale(0%);
}

https://jsfiddle.net/q5pdemjh/1/ https://jsfiddle.net/q5pdemjh/1/

enjoy!请享用!

You could do something like this:你可以这样做:

.one, .two {
  -webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */
  filter: grayscale(100%);
  margin-left: 19px;
  padding-top: 10px;
  padding-bottom: 10px;
  transition: all 0.5s;
}

.one:hover, .two:hover {
  filter: grayscale(0);
  -webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */
}

The added transition: all 0.5s ensures that going from grayscale(100%) to grayscale(0) and back is smoothed over a 0.5s timeframe, whereas the :hover pseudoelement allows you to only trigger the change when you hover the divs with classes .one and .two .添加的transition: all 0.5s确保在 0.5 秒的时间范围内从grayscale(100%)grayscale(0)并返回平滑,而:hover伪元素允许您仅在将 div 与类悬停时触发更改.one.two

You can use this code.您可以使用此代码。

 .one, .two { -webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */ filter: grayscale(100%); margin-left: 19px; padding-top: 10px; padding-bottom: 10px; transition: all .5s ease; } .two { padding-top: 10px; } .one { padding-bottom: 5px; } .example { width: 100px; background-color: black; position: absolute; top: 0; left: 0; } .a:hover .one, .b:hover .two { -webkit-filter: grayscale(0%); /* Safari 6.0 - 9.0 */ filter: grayscale(0%); }
 <div class="example"> <div class="a"> <img src="https://sep.yimg.com/ay/filmandvideolighting/times-square-106-primary-red-lighting-gel-sheet-10x10-in-1.jpg" class="one" width="60" /> </div> <div class="b"> <img src="https://sep.yimg.com/ay/filmandvideolighting/times-square-106-primary-red-lighting-gel-sheet-10x10-in-1.jpg" class="two" width="60" /> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM