JQuery改变<img>的色调,饱和度,伽玛?

Tom*_*kay 9 jquery image colors jquery-plugins hover

如果将鼠标悬停在图像上,我想让图像变暗.是否可以使用JQuery(或Javascript)更改img的色调,饱和度或灰度系数?

nik*_*ksy 11

你尝试过PaintbrushJSPixastic库吗?


Mar*_*tos 10

<div>在图像顶部显示和隐藏半透明黑色.


Nic*_*ver 6

不能使用JavaScript(因为它无法操作二进制文件)来执行此操作,但是您可以使用HTML5 <canvas>元素来帮助完成此操作.

看看这里,有一些图书馆可以提供帮助.

如果您只是想淡化它,请在悬停时更改不透明度,例如:

$("img").css({ opacity: 0.5 }).hover(function() {
  $(this).animate({ opacity: 1 });
}, function() {
  $(this).animate({ opacity: 0.5 });
});
Run Code Online (Sandbox Code Playgroud)


小智 5

我把这个共同的原型,采用了跨浏览器,CSS唯一的解决方案,以在悬停动画图像的饱和度.在JS/jQuery中有很多方法可以做到这一点,但是为什么不在CSS中做呢?

img[class*="filter-resat"] {
    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"); // For Firefox 10+
    filter: gray; // For IE 6+
    -webkit-filter: grayscale(100%); // for Chrome & Safari

    // Now we set the opacity
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=60)"; // for IE
    filter: alpha(opacity=40); // Chrome + Safari
    -moz-opacity: 0.6; // Firefox
    -khtml-opacity: 0.6; // Safari pre-webkit
    opacity: 0.6; // Modern

    // Now we set up the transition
    -webkit-transition: all 1.0s ease;
    -webkit-backface-visibility: hidden;
}

img[class*="filter-resat"]: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%);
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
    filter: alpha(opacity=100);
    -moz-opacity: 1;
    -khtml-opacity: 1;
    opacity: 1;
}

// You can leave this part out and the above code will default to a 1 second transition duration.
img.filter-resat9 {-webkit-transition: all .9s ease;}
img.filter-resat8 {-webkit-transition: all .8s ease;}
img.filter-resat7 {-webkit-transition: all .7s ease;}
img.filter-resat6 {-webkit-transition: all .6s ease;}
img.filter-resat5 {-webkit-transition: all .5s ease;}
img.filter-resat4 {-webkit-transition: all .4s ease;}
img.filter-resat3 {-webkit-transition: all .3s ease;}
img.filter-resat2 {-webkit-transition: all .2s ease;}
img.filter-resat1 {-webkit-transition: all .1s ease;} 
Run Code Online (Sandbox Code Playgroud)

检查出DEMO这里和的jsfiddle这里

http://jsfiddle.net/buttonpresser/x6GfX/show/light/