views:

8478

answers:

10

I'm trying to use animate function to change the height and opacity for a DIV. The div has a image background in CSS, it works fine on Firefox and Safari, but when i test it on 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 it?

+8  A: 

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});
Eric
You're correct. jQuery takes care of the cross-browser differences when it comes to opacity.
J-P
A: 

Do you use some pngfix script ? that may be the culprit.

Olivvv
A: 

You can use fadeTo to accomplish what you want to do:

$('#list_box').fadeTo("slow", 0.33);

fadeIn and fadeOut do transitions from 0 to 100%, but the above will allow you to fade to an arbitrary opacity.

(http://docs.jquery.com/Effects/fadeTo#speedopacitycallback)

Andy Mikula
+1  A: 

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.

Jeremy Conescu
+3  A: 

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.

Doug Neiner
Do you have a link to more information about this? I had a feeling this was going to be the solution (i.e., no solution in IE) but would like more information. Also, as long as I'm listing things I'd like, someone from the IE team to throttle will do nicely.
Dereleased
+1  A: 

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.

newfront
+1  A: 

Very (very) late with the answer, but as this is at the top of Google when I looked for help with a jquery v animate issue in IE8 I thought i'd post it here.

My problem was connected to the hasLayout bug in IE, and adding "display: inline-block" to the element to be faded fixed the problem.

katebp
A: 

Ok this might help a little bit, I found a solution in this site about the exact problem http://blog.bmn.name/2008/03/jquery-fadeinfadeout-ie-cleartype-glitch/

in conclusion, the general problem is the opacity filter on IE, in your specific case there is not much you can do, thought

but in case you fade in and out, the prevent the problem with a png background image you just have to remove the filter attribute the jQuery function added whe the fx ends. Just use a callback function, something like that would do it:

$('#node').fadeOut('slow', function() {
  this.style.removeAttribute('filter');
});

in case you selectors returns more than one, use the each function, something like this:

$('.nodes').fadeIn('fast',
  function() {
  $(this).each (
    function(idx,el) {
    el.style.removeAttribute('filter');
    }
  );
}
);

hope this helps

y0ux
+1  A: 

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.

Hoxxy
Thank you ! Adding 'float: left' fixed opacity for me and reinforced my dislike for IE in the process :)
Laurent Alquier