用于IE 10的CSS过滤器灰度图像

用于IE 10的CSS过滤器灰度图像,css,filter,internet-explorer-10,grayscale,Css,Filter,Internet Explorer 10,Grayscale,有没有一种方法可以使灰度图像过滤器在没有JavaScript或SVG的情况下在IE10上工作 除了IE 10之外,我已经在所有浏览器中成功地使用了以下代码 img{ filter:url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'0.333

有没有一种方法可以使灰度图像过滤器在没有JavaScript或SVG的情况下在IE10上工作

除了IE 10之外,我已经在所有浏览器中成功地使用了以下代码

img{
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"); /*     Firefox 10+, Firefox on Android */
filter:gray; /* IE6-9 */
-webkit-filter:grayscale(100%); /* Chrome 19+, Safari 6+, Safari 6+ iOS */
-moz-filter: grayscale(100%);
-o-filter: grayscale(100%);}
img{
过滤器:url(“数据:图像/svg+xml;utf8,#灰度”);/*Firefox 10+,Android上的Firefox*/
过滤器:灰色;/*IE6-9*/
-webkit过滤器:灰度(100%);/*Chrome 19+、Safari 6+、Safari 6+iOS*/
-moz滤波器:灰度(100%);
-o型过滤器:灰度(100%);}

这种方法只适用于WebKit和Firefox。它在IE或歌剧中不起作用。WebKit是目前唯一支持CSS过滤器的浏览器,而Firefox支持HTML上的SVG过滤器。Opera和IE支持SVG过滤器,但仅支持SVG内容

在IE10中没有很好的方法来实现这一点,因为它放弃了对传统IE过滤器的支持。不过,有一种方法我不推荐

可以使用头部中的以下元元素将IE10设置为使用IE9标准模式进行渲染:

<meta http-equiv="X-UA-Compatible" content="IE=9">

这将重新启用对旧IE过滤器的支持。然而,它的副作用是让IE进入IE9模式,IE10的最新进展将不再得到支持。在您的情况下,您目前可能不需要这些新功能,但如果您沿着这条路走下去,您需要在将来更新站点时意识到这一点


以上链接为>IE10和其他浏览器提供了一个好主意。

这是一个使用HTML5和使用淡入效果的jquery的跨浏览器解决方案

代码:


//窗口加载。这将等待图像加载完成,这是必不可少的
$(窗口)。加载(函数(){
//淡入图像,这样就不会加载彩色“弹出”文档,然后再加载窗口
美元(“.item img”).fadeIn(500);
//克隆图像
$('.item img')。每个(函数(){
var el=$(本);
css({“位置”:“绝对”}).wrap(“”.clone().addClass('img_grayscale').css({“位置”:“绝对”,“z索引”:“998”,“不透明度”:“0”}).insertBefore(el).queue(函数(){
var el=$(本);
el.parent().css({“width”:this.width,“height”:this.height});
el.dequeue();
});
this.src=灰度(this.src);
});
//淡入图像
$('.item img').mouseover(函数(){
$(this.parent().find('img:first').stop().animate({opacity:1},1000);
})
$('.img_grayscale').mouseout(函数(){
$(this.stop().animate({opacity:0},1000);
});     
});
//灰度w画布法
函数灰度(src){
var canvas=document.createElement('canvas');
var ctx=canvas.getContext('2d');
var imgObj=新图像();
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);
对于(变量y=0;y
这将不起作用:Firefox 35刚刚发布,现在支持CSS过滤器。虽然这些链接可能会回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能会无效。虽然这些链接可能会回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能无效。添加了代码。祝你好运
<script src="jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

    // On window load. This waits until images have loaded which is essential
    $(window).load(function(){

        // Fade in images so there isn't a color "pop" document load and then on window load
        $(".item img").fadeIn(500);

        // clone image
        $('.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);
        });

        // Fade image 
        $('.item img').mouseover(function(){
            $(this).parent().find('img:first').stop().animate({opacity:1}, 1000);
        })
        $('.img_grayscale').mouseout(function(){
            $(this).stop().animate({opacity:0}, 1000);
        });     
    });

    // Grayscale w canvas method
    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>