css3在页面加载时淡入,在几秒钟后输出

Ion*_*cap 4 html javascript css jquery css3

我一直在stackoverflow搜索一段时间的答案,但在我看来这是没有被质疑过.

如果我可能错过了某个地方的答案,但在这里它是:

所以我正在处理一个在页面加载时淡入div的页面,现在我想在几秒钟之后淡出它.我似乎无法找到完成这项工作的正确方法.

@-webkit-keyframes fadein {
from {
opacity:0;
}
to {
opacity:1;
}
}
@-webkit-keyframes fadeout {
from {
    opacity:1;
}
to {
    opacity:0;
}
}
div {
width: 400px;
margin: 0 auto;
text-align: center;
-webkit-animation:fadein 1s;
-webkit-animation:fadeout 1s;
-webkit-animation-delay:fadeout 5s;
}
Run Code Online (Sandbox Code Playgroud)

html:

 <div>
 <h1><font size="+6"> :(</font></h1><br />
 <h1>Whoops<span>Something went wrong</span></h1><br />
 <h1><span><div id="timer_div">you will be redirected in</div> seconds</span></h1>
 </div>
Run Code Online (Sandbox Code Playgroud)

mar*_*ebl 9

您的问题源于您实际想要按顺序运行两个动画.为了使这项工作可靠,您有两种选择:

仅限CSS: http ://jsfiddle.net/marionebl/M9LR6/

注意opacity: 0;在动画完成时保持消息隐藏.另外:这不适用于IE <= 9,它不支持关键帧动画:http://caniuse.com/#feat=css-animation

@keyframes fadeInOut {
    0% {
        opacity: 0;
    }
    16% {
       opacity: 1;
    }
    84% {
       opacity: 1;
    }
    100% {
       opacity: 0;
    }
}

.message {
    width: 400px;
    margin: 0 auto;
    opacity: 0;
    text-align: center;
   -webkit-animation: fadeInOut 6s;
   animation: fadeInOut 6s;
}
Run Code Online (Sandbox Code Playgroud)

涉及JS: http ://jsfiddle.net/marionebl/P26c9/1/

更灵活,更容易更改,支持IE9.

CSS:

@-webkit-keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}
@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@-webkit-keyframes fadeOut {
    from {
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}
@keyframes fadeOut {
    from {
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}

.fadeIn {
    -webkit-animation: fadeIn;
    animation: fadeIn;
    opacity: 1;
}

.fadeOut {
    -webkit-animation: fadeOut;
    animation: fadeOut;
    opacity: 0;
}

.fast {
    -webkit-animation-duration: 1s;
    animation-duration: 1s
}

.message {
    width: 400px;
    margin: 0 auto;
    text-align: center;
}
Run Code Online (Sandbox Code Playgroud)

JS:

var $message = $('.message');
$message.addClass('fadeIn fast');

setTimeout(function(){
   $message.removeClass('fadeIn').addClass('fadeOut');
}, 5000);
Run Code Online (Sandbox Code Playgroud)