jQuery如何在IE8及以下版本实现“淡入淡出”效果?

6

我想知道jQuery如何在IE浏览器中生成一个淡入淡出的效果,因为它们不支持opacity属性。在其他浏览器(如Firefox和Chrome)中,使用动画来控制opacity实现淡入淡出。

我查看了代码,但实话说,我找不到任何能理解的内容!

2个回答

9
从jQuery源代码中,它们基本上检测是否支持透明度,如果不支持,则使用IE的alpha滤镜。
if ( !jQuery.support.opacity ) {
jQuery.cssHooks.opacity = {
    get: function( elem, computed ) {
        // IE uses filters for opacity
        return ropacity.test( (computed && elem.currentStyle ? elem.currentStyle.filter : elem.style.filter) || "" ) ?
            ( parseFloat( RegExp.$1 ) / 100 ) + "" :
            computed ? "1" : "";
    },

    set: function( elem, value ) {
        var style = elem.style,
            currentStyle = elem.currentStyle;

        // IE has trouble with opacity if it does not have layout
        // Force it by setting the zoom level
        style.zoom = 1;

        // Set the alpha filter to set the opacity
        var opacity = jQuery.isNaN( value ) ?
            "" :
            "alpha(opacity=" + value * 100 + ")",
            filter = currentStyle && currentStyle.filter || style.filter || "";

        style.filter = ralpha.test( filter ) ?
            filter.replace( ralpha, opacity ) :
            filter + " " + opacity;
    }
};
}

3
使用以下样式 filter:alpha(opacity=40)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接