我有很多幻燈片顯示每個項目。我想讓所有未選擇的幻燈片顯示爲灰色(不僅可以淡化它們的不透明度,也可以在它們上面覆蓋乳白色覆蓋層,我可以做到這一點),使當前選定的幻燈片在視覺上脫穎而出。它應該在當前的webkit瀏覽器上運行,並從IE8向上運行。我試圖與畫布合作將它們變灰,但它會打破我網站上的幻燈片和其他內容。有沒有另一種方法通過CSS & jQuery灰化圖像/幻燈片?CSS/jQuery:灰色jQuery循環彩色圖像幻燈片
如果只是這樣-webkit-filter: grayscale(100%);
會在Safari,Chrome瀏覽器,Firefox和IE瀏覽器> = 8 ...
<script type="text/javascript">
$(window).load(function(){
$('.item img').each(function(){
var el = $(this);
el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
var el = $(this);
el.parent().css({"width":this.width,"height":this.height});
el.dequeue();
});
this.src = grayscale(this.src);
});
$('.item img').mouseover(function(){
$(this).parent().find('img:first').stop().animate({opacity:1}, 1000);
})
$('.img_grayscale').mouseout(function(){
$(this).stop().animate({opacity:0}, 1000);
});
});
function grayscale(src){
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var imgObj = new Image();
imgObj.src = src;
canvas.width = imgObj.width;
canvas.height = imgObj.height;
ctx.drawImage(imgObj, 0, 0);
var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
for(var y = 0; y < imgPixels.height; y++){
for(var x = 0; x < imgPixels.width; x++){
var i = (y * 4) * imgPixels.width + x * 4;
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2])/3;
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
return canvas.toDataURL();
}
</script>
謝謝,我將實現一個。 – Systembolaget 2012-08-13 09:08:41