css3 让图片变成灰色(filter),但针对IE11浏览器无效
大家讲道理
大家讲道理 2017-04-17 11:30:08
0
4
681

想要设置网站图片默认为灰色,鼠标经过时显示为彩色

在网上找了很多方法, 修改css后,在非IE浏览器内都可以正常显示为灰色,但在IE11浏览器内一直为彩色
查看好多教程,说是IE6-9都可以,但更高版本就不行了,有什么好办法没有

代码如下:

{
-webkit-filter: grayscale(1);
-moz-filter: grayscale(1);
filter: grayscale(1);
-ms-filter: grayscale(1);
-o-filter: grayscale(1);
filter: gray;
}

请各位帮忙看看是什么原因

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(4)
刘奇

If the image format is not SVG, the filter is not supported on IE browser.
If it is svg, filter is only supported on ie10 and 11.
See caniuse for details

迷茫

It may be that the current IE version is not compatible with some CSS3 properties

洪涛

svg or canvas

Peter_Zhu

Can use canvas

var imgObj = document.getElementById('imgToGray');   
  
function gray(imgObj) {  
    var canvas = document.createElement('canvas');  
    var canvasContext = canvas.getContext('2d');  
  
    var imgW = imgObj.width;  
    var imgH = imgObj.height;  
    canvas.width = imgW;  
    canvas.height = imgH;  
  
    canvasContext.drawImage(imgObj, 0, 0);  
    var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);  
  
    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;  
        }  
    }  
    canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);  
    return canvas.toDataURL();  
}  
imgObj.src = gray(imgObj);  
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template