简体   繁体   English

动画不透明度在IE上无法正常工作

[英]Animate opacity doesn't work properly on IE

I'm trying to use animate() to change the height and opacity of a div . 我正在尝试使用animate()来改变div的高度和不透明度。 The div has an image background in CSS. div在CSS中有图像背景。 It works fine on Firefox and Safari, but when I test it in IE the background is being removed. 它在Firefox和Safari上运行良好,但是当我在IE中测试时,背景正在被删除。 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? 我该如何解决这个问题?

I was under the impression that jQuery did the whole opacity support thing for you. 我的印象是jQuery为你做了整个不透明度的支持。 Does this work for all browsers? 这适用于所有浏览器吗?

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

You do not need to write a special handler for IE, jQuery does it all for you behind the scenes: 你不需要为IE编写一个特殊的处理程序,jQuery在幕后为你做了一切:

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. 但是 :如果你的背景图像有24位透明PNG正在消失,你需要注意你不能将filter: alpha (jQuery在IE中正确使用后面)与24位透明PNG结合使用IE7或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 我相信唯一的方法是在你使用filter: alpha的对象上设置背景颜色( transparent除外) 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: 如何测试 background-image声明后,通过在CSS中添加类似的东西,将#list_box上的背景颜色设置为纯色:

#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. 如果背景图像仍然存在,并且您的#list_box动画正确(除了可怕的背景),您就知道问题是什么,并且必须找到另一种方法来完成您想要的任务。

I've been having the same problem. 我一直有同样的问题。 I stumbled into the answer, when I set the opacity to 40%: 当我将不透明度设置为40%时,我偶然发现了答案:

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

I noticed that made the opacity jump to 100%, then animate down to 40%. 我注意到不透明度跳跃到100%,然后动画降低到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. 除了文字在IE浏览器中看起来仍然很糟糕之外,动画也很流畅。

To clean up the text, I removed the opacity from the css in IE after the animation. 为了清理文本,我在动画后从IE中的css中删除了不透明度。 This seems to clear up the text quite a bit in IE6 and IE8. 这似乎在IE6和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. 我在Parallels的Mac上,在IE6和IE8中测试它。 Everything seems to work fine on the Mac side. 一切似乎在Mac方面都运行良好。

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. 非常(非常)迟到的答案,但是因为当我在IE8中寻找jquery v animate问题的帮助时,这是谷歌的顶部,我想我会在这里发布它。

My problem was connected to the hasLayout bug in IE, and adding "display: inline-block" to the element to be faded fixed the problem. 我的问题与IE中的hasLayout错误有关,并且将“display:inline-block”添加到要褪色的元素中修复了问题。

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; 我只是添加了float:left; to the #nav li css and it fixed the issue. 到#nav li css并解决了这个问题。

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 在jQuery中,一旦div被设置为在IE中具有不透明度:0(在标准兼容浏览器中)或过滤器:alpha(opacity = 0),您可以使用

$('#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. 由于jQuery支持跨浏览器支持,如果你最终通过IE动画过滤器,那么jQuery可能会尝试支持IE,并且当jQuery触发不透明度变化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. 我有同样的问题,加上IE的奇怪问题是无法处理包含多个项目的div堆栈的淡入淡出。

I noticed the problem was caused by position:relative of the container. 我注意到问题是由位置引起的:容器的相对关系。 If "switching" to absolute opacity animation will work. 如果“切换”到绝对不透明度动画将起作用。

I solved it with adding an opaque background to the animated element: 我通过向动画元素添加不透明背景解决了这个问题:

CSS: CSS:

.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 195px;
    height: 274px;
    cursor: pointer;
    background: #fff url('../images/common/image_hover.png') 0 0 no-repeat; /* the solution */
    opacity: 0;
    -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; /* IE8 */
    filter: alpha(opacity=0); /* IE6-7 */     
    zoom: 1;
}

JS: JS:

$('.overlay').hover(
    function(){
        $(this).animate({'opacity': 0.7}, 300);
    },
    function(){
        $(this).animate({'opacity': 0}, 250);
    }
);

Works for IE7-8 适用于IE7-8

Hope this will help someone ;) 希望这会对某人有所帮助;)

I´ve had the same problem with the IE 7, the problem was a trailing comma after the opacity property 我在IE 7中遇到了同样的问题,问题是在opacity属性之后有一个尾随的逗号

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

It has to be: 它一定要是:

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

I found a solution that worked for me: position:inline-block; 我找到了一个适合我的解决方案: position:inline-block; This works for fading text opacity, I haven't tried it with a CSS background image. 这适用于淡化文本不透明度,我还没有尝试过使用CSS背景图像。 Maybe it helps anyway. 无论如何它可能有帮助。

I just wanted to report a small bug with fadeTo method in Internet Explorer 8. It won't work if your element as "display" set to "inline". 我只是想在Internet Explorer 8中使用fadeTo方法报告一个小错误。如果您的元素“display”设置为“inline”,则无效。 I found that you need to put it to "inline-block" and then it works perfectly. 我发现你需要把它放到“内联块”然后才能完美运行。 There is nothing about this on the web and it's not the first time I have this problem. 网上没有这个,这不是我第一次遇到这个问题。

Don't know if it's the right way to report this issue, but i'm sure someone will read this post :) 不知道这是报告这个问题的正确方法,但我相信有人会读这篇文章:)

found at http://www.devcomments.com/IE-8-fadeTo-problem-with-inline-elements-to65024.htm 发现于http://www.devcomments.com/IE-8-fadeTo-problem-with-inline-elements-to65024.htm

You can use fadeTo to accomplish what you want to do: 您可以使用fadeTo来完成您想要做的事情:

$('#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. fadeInfadeOut从0到100%进行过渡,但上面的内容将允许您淡入任意不透明度。

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

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/ 好吧这可能有点帮助,我在这个网站上找到了一个关于确切问题的解决方案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 总之,一般的问题是IE上的不透明度过滤器,在你的具体情况下你可以做的事情并不多,想一想

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. 但是如果你淡入淡出,使用png背景图像来防止问题你只需要删除fx结束时添加的jQuery函数的filter属性。 Just use a callback function, something like that would do it: 只需使用回调函数,就像这样:

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

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');
             }
        );
     }
);

Same problem with IE8. 与IE8相同的问题。 Adding "display: inline-block" to .hover2 in fixed the problem. 将“display:inline-block”添加到.hover2以解决问题。

$(function() {

        $(".hover1").css("opacity","1.0"); // Default set opacity to 1.0

        // On Mouse over
        $(".hover1").hover( 
                            function () {

                                        // SET OPACITY TO 15%
                                        $("span.hover2").stop().animate({opacity: 0.15}, 1200);
                                        },

                                        // ON MOUSE OUT
                            function () {

                                        // SET OPACITY BACK TO 100%
                                        $("span.hover2").stop().animate({opacity: 1.0}, 1200);
                                        }
                         );
                }
     );

Do you use some pngfix script ? 你使用一些pngfix脚本吗? that may be the culprit. 这可能是罪魁祸首。

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

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