简体   繁体   English

IE8不透明度在png透明上设置动画

[英]IE8 opacity animate on a png transparent

I try to animate the opacity of a div which have a .png background. 我尝试为具有.png背景的div设置不透明度动画。

<div id="image" style="background:url('background.css') repeat-x";></div>

For that, I use this code : 为此,我使用以下代码:

$("#image").stop().animate({
    opacity: 1
    }, 1000, "easeInOutSine", function() {

    $("#image").stop().animate({
        opacity: 0
        }, 1000, "easeInOutSine");

});

It's ok for recent browsers. 对于最近的浏览器来说还可以。 But in IE 7 and 8, the transparence is black... I looked for a solution but none works. 但是在IE 7和8中,透明胶片是黑色的...我正在寻找解决方案,但没有一个有效。 Someone can help me? 有人可以帮我吗?

Thanks 谢谢

IE8 and earlier have really really bad support for opacity. IE8和更早的版本对不透明度的支持确实很差。

The support they do have for it is non-standard and full of bugs. 他们对此提供的支持是非标准的,并且存在很多错误。 You can't use the standard opacity style for them at all; 您根本不能对它们使用标准的opacity样式; they have to use the non-standard filter style, and it's not good. 他们必须使用非标准的filter样式,这不是很好。 Doing stuff like animating it is never going to work well, if you can get it working at all. 如果您可以使动画工作,那么像动画这样的事情永远都不会奏效。

The best advice I can give is to just accept that these are old browsers and don't support modern features like this. 我能提供的最佳建议是,仅接受这些是旧版浏览器,并且不支持此类现代功能。 Provide a fallback for them that doesn't involve animating it. 为他们提供一个不涉及动画的后备。

I wrote this since its hard to find a good solution. 我写这篇是因为它很难找到一个好的解决方案。 This one detects lower than IE9 and fixes all images that you want to animate (with opacity). 该图像检测到低于IE9的图像,并修复了所有要设置动画(不透明)的图像。

/* 
1. Include jQuery , for example 1.8.3.
2. Give the images class 'fixIE8'.
3. Save an full transparent image named 'trans.png'. */


function fixIE8() {
        $(".fixIE8").each(function(){

        var imageWidth  = $(this).width();
        var imageHeight = $(this).height();
        var imageSource = $(this).attr('src');
        var imageStyle  = $(this).attr('style');

        $(this).attr('width', imageWidth);
        $(this).attr('height', imageHeight);
        $(this).attr('src', 'trans.png');
        $(this).attr('style', ''+imageStyle+' background: transparent; background-image:none; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+imageSource+'"); }');

        })
    }

    function IEversion() {

        var userHeader = navigator.userAgent;

        if (userHeader.indexOf('MSIE') !== -1) {
            var MSIE            = userHeader.indexOf('MSIE');
            var versionAt       = MSIE + 5;
            var browserVersion  = userHeader.charAt(versionAt);
            return browserVersion;
        }
    }

    $(document).ready(function(){
        if (IEversion() < 9) { fixIE8(); }
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM