Animate opacity doesn't work properly on IE

2020-02-16 08:46发布

I'm trying to use animate() to change the height and opacity of a div. The div has an image background in CSS. It works fine on Firefox and Safari, but when I test it in IE the background is being removed. This is my code:

if (jQuery.support.opacity) {
    jQuery('#list_box').animate({opacity: '1',height: '300px',top: newTop},{duration: 300});
} else {
    jQuery('#list_box').animate({filter: 'alpha(opacity=100)',height: '300px',top: newTop},{duration: 300});
}

How can I fix this problem?

14条回答
Summer. ? 凉城
2楼-- · 2020-02-16 09:24

I've been having the same problem. I stumbled into the answer, when I set the opacity to 40%:

$('#list_box').stop().animate({opacity: '.4'},"slow");

I noticed that made the opacity jump to 100%, then animate down to 40%. Eureka.

So, now I explicitly set the opacity to zero before the animation:

$('#list_box').css({opacity:0}).stop().animate({opacity: '1'},"slow");

That animates smoothly, except the text still looks horrible in IE.

To clean up the text, I removed the opacity from the css in IE after the animation. This seems to clear up the text quite a bit in IE6 and IE8.

$('#list_box').css({opacity:0}).stop().animate({opacity: '1'},"slow",function(){
    //remove the opacity in IE
    jQuery.each(jQuery.browser, function(i) {
        if($.browser.msie){
            $('#list_box').css({opacity:''});
        }
    });
});

I'm testing it on a Mac in Parallels, in IE6 and IE8. Everything seems to work fine on the Mac side.

查看更多
家丑人穷心不美
3楼-- · 2020-02-16 09:26

You do not need to write a special handler for IE, jQuery does it all for you behind the scenes:

jQuery('#list_box').animate({opacity: '1',height: '300px',top: newTop}, 300);

HOWEVER: If you have a 24-bit transparent PNG as your background image that is disappearing, you need to be aware that you cannot combine filter: alpha (which jQuery correctly uses behind the scenes in IE) with a 24-bit transparent PNG in IE7 or IE8. I believe the only way around it is to set a background color (other than transparent) on the object on which you are using filter: alpha

How to test: Simply set a background color on #list_box to a solid color by adding something like this to your CSS after your background-image declaration:

#list_box { background-color: red }

If the background image remains, and your #list_box animates correctly (except for the hideous background) you know what the problem is and will have to find another way to accomplish what you want.

查看更多
我想做一个坏孩纸
4楼-- · 2020-02-16 09:27

I was under the impression that jQuery did the whole opacity support thing for you. Does this work for all browsers?

$('#list_box').animate({opacity: '1',height: '300px',top: newTop},{duration: 300});
查看更多
走好不送
5楼-- · 2020-02-16 09:31

I had the same sort of issue with this:

$('#nav li').hover(function() {
 $(this).stop().animate({opacity: '0.4'}, 'slow');
},
function() {
 $(this).stop().animate({opacity: '1'}, 'slow');
});

I simply added float:left; to the #nav li css and it fixed the issue.

查看更多
SAY GOODBYE
6楼-- · 2020-02-16 09:31

I´ve had the same problem with the IE 7, the problem was a trailing comma after the opacity property

jQuery(this).animate({opacity:1.00,},800);

It has to be:

jQuery(this).animate({opacity:1.00},800);
查看更多
Lonely孤独者°
7楼-- · 2020-02-16 09:33

In jQuery, once the div is set to have either opacity:0 (in Standards Compliant Browsers) or filter:alpha(opacity=0) in IE, you can just use

$('#div').animate({opacity:1},100);
Since jQuery supports cross-browser support, if you end up animating the filter via IE, then chances are jQuery is trying to support IE and the conflict comes when jQuery fires the opacity change x2.

I hope this helps. I have had the same issue, plus odd issues with IE not being able to handle fading on a div stack with multiple items in it.

查看更多
登录 后发表回答