html - 悬停时返回颜色

标签 html css

我在这里创建了 3 张从彩色到灰度的图像,我想在悬停时显示颜色我做错了什么?

这是 fiddle 链接:

http://jsfiddle.net/4tHWg/6/

CSS 代码:

 .box {
       float: left;
       position: relative;
       width: 14.285714286%;
 }
  
 .boxInner img {
       width: 100%;
       display: block;
 }

 .boxInner img:hover {
      -webkit-filter: grayscale(0%);
 }

 @-webkit-keyframes toGrayScale {
    to { -webkit-filter: grayscale(100%);  }
 }

 .box:nth-child(1) img {
    -webkit-animation: toGrayScale 1s 0.5s forwards;
 }

.box:nth-child(2) img {
    -webkit-animation: toGrayScale 2s 1s forwards;
}

.box:nth-child(3) img {
    -webkit-animation: toGrayScale 3s 1.5s forwards;
}

最佳答案

还有另一个简单的解决方案。

.grayscale {
filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\'/></filter></svg>#grayscale");
filter: gray;
-webkit-filter: grayscale(100%);}

.grayscale:hover {
filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'1 0 0 0 0, 0 1 0 0 0, 0 0 1 0 0, 0 0 0 1 0\'/></filter></svg>#grayscale");
-webkit-filter: grayscale(0%);}

关于html - 悬停时返回颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19891591/

相关文章:

html - Css 媒体查询解决问题

html - jQuery Mobile 文本框的宽度

css - 为什么在这个html页面中有水平滚动条

css - 在一行中显示 3 个 DIV

javascript - IE8 在触发点击 Javascript 事件时将一些边距重置为 0

javascript - href 菜单栏上的下拉按钮

android - 我可以在 Android 2.1 的基于 Webkit 的浏览器中使用数据 URL 吗?

javascript - HTML 中的 Angular 6 如果字段未定义则显示不适用

css - 数据切换上的弹出框未在不同版本的 Bootstrap 中呈现

javascript - 隐藏文件上传按钮