简体   繁体   English

jQuery fadeOut()在CSS淡入后跳过

[英]jQuery fadeOut() skips after CSS fade in

I'm having a bit of a frustrating problem regarding fading out an element using jQuery after it has been faded in with CSS. 在CSS淡入后,使用jQuery淡出元素时,我遇到了一个令人沮丧的问题。 I set up a CSS animation to fade in an element when the page loads using the following (I've also got the relevant browser prefixes included too, I'm using Stylus): 我设置了一个CSS动画中的元素淡出时使用以下页面加载(我也得到了相关的浏览器前缀包括过,我使用触控笔):

@keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
.elem {
    opacity: 0;
    animation: fadein 500ms ease-in 1ms forwards;
}

My issue is that when an event handler is activated that runs the following, the fadeOut does not fade but instead skips straight to nothing: 我的问题是,激活运行以下命令的事件处理程序时,fadeOut不会淡出,而是直接跳过:

    $('.elem').fadeOut(400, function(){
        $('.elem').fadeIn(400);
    });

I've been able to replicate the issue in this JSFiddle . 我已经能够在此JSFiddle中复制该问题。 Can anyone help me out? 谁能帮我吗? :) Thanks a lot! :) 非常感谢!

I would say it's conflicting with the CSS you're using. 我会说这与您使用的CSS冲突。 jQuery is probably using other opacity related properties than what your CSS is. jQuery可能正在使用与CSS不一样的其他与不透明度相关的属性。 An all jQuery solution might be this: 所有jQuery解决方案可能是这样的:

CSS 的CSS

.elem {
    display: none;
}

jQuery jQuery的

$('.elem').fadeIn(1000); // on page load, fade in with jQuery

$('#go').click(function(){
    $('.elem').fadeOut(400, function(){
        $('.elem').fadeIn(400);
    });
});

Related: Conflict between CSS transition and jQuery fade 相关: CSS过渡和jQuery淡入之间的冲突

I know you asked for FadeIn and FadeOut ... here's your code with animate and opacity instead. 我知道您要求FadeInFadeOut ...这是带有animateopacity代码。

$('#go').click(function(){
    $('.elem').css('animation','none').animate({
        'opacity' : 0
        },function(){
            $('.elem').animate({
                'opacity' : 1
                });
        });
    });

It seems that the css-animation has higher precedence than the inline style that jQuery applies to the element when fadeOut is used. 似乎css-animation的优先级高于使用fadeOut时jQuery应用于元素的内联样式。 This means the animation's opacity : 1; 这意味着动画的opacity : 1; overrides any opacity setting jQuery applies, up until jQuery sets the element to display:none; 覆盖jQuery应用的所有不透明度设置,直到jQuery将元素设置为display:none;为止display:none;

You can do this to get around: 您可以这样做来解决:

$('#go').click(function(){

   $('.elem').css('animation','none').fadeOut(400, function(){
      $('.elem').fadeIn(400);
    });

});

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

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