【问题标题】:CSS filter grayscale image for IE 10IE 10 的 CSS 过滤灰度图像
【发布时间】:2013-04-30 17:04:35
【问题描述】:

有没有办法让灰度图像过滤器在没有 JavaScript 或 SVG 的 IE 10 上工作?

我已经在除 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%);}

【问题讨论】:

    标签: css filter internet-explorer-10 grayscale


    【解决方案1】:

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

    Code

    Demo

    代码:

    <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>
    

    【讨论】:

    • 虽然这些链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    • 已添加代码。祝你好运。
    【解决方案2】:

    【讨论】:

    • 虽然这些链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    【解决方案3】:

    此方法仅适用于 WebKit 和 Firefox。它在 IE 或 Opera 中不起作用。 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 中的最新进展。在您的情况下,您目前可能不需要这些新功能,但如果您沿着这条路走,将来更新网站时需要注意它。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 2014-02-11
    • 2013-04-26
    • 1970-01-01
    • 2013-01-26
    相关资源
    最近更新 更多