Correcting IE Cleartype/Filter Problem when Animating Opacity of Text with jQuery

假如想象 提交于 2019-12-05 22:22:05

Add this to a new js file, say jquery.fadefix.js:

jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 

jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 

jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
};

and include this file after the main jquery script in your page.

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.fadefix.js"></script>

Fade will now work as expected in IE.

Dave Patel

Easier to do this:

$("#sample").fadeIn(1000,function(){this.style.removeAttribute("filter");});

or

$("#sample").fadeOut(1000,function(){this.style.removeAttribute("filter");});

or

$("#sample").show(1000,function(){this.style.removeAttribute("filter");});

or

$("#sample").hide(1000,function(){this.style.removeAttribute("filter");});

Had this same exact problem. I was racking my brain trying to figure out why this solution was working for everyone else, but not me.

The solution I found was to add the "filter" to the animate() function instead. So, using your original function as the starting point:

$('#sample').animate({'opacity':1.0,'filter':''},500);

That worked perfectly for me. Hopefully this helps someone else who has been Googling this like me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!