我该怎么做?我尝试使用 css3 只是出于性能原因(淡入可能会变得有点断断续续)。
现在它们都同时发生。
function fadeInPlaylist(elem) {
elem.css('opacity',1);
}
$(window).load(function() {
$('.playlist').each(function(i) {
setTimeout(fadeInPlaylist($(this)),2500*i);
});
});
您不正确地调用setTimeout
。
setTimeout(fadeInPlaylist($(this)),2500*i);
应该是:
setTimeout(function(){fadeInPlaylist($(this));},2500*i);
另外,这里有一个工作小提琴:http://jsfiddle.net/q7Wa8/
如果您确实需要仅使用 CSS3 执行此操作,请使用以下代码:
@keyframes reset {
0% { opacity: 0; }
100% { opacity: 0; }
}
@keyframes fade-in {
0% { opacity: 0; }
60% { opacity: 0; }
100% { opacity: 1; }
}
.playlist {
animation-name: reset, fade-in;
animation-duration: 2.5s;
animation-timing-function: ease-in;
animation-iteration-count: 1;
animation-delay: 0, 0;
}
但是您会遇到兼容性问题。这是跨浏览器代码,在IE中不起作用:
@keyframes reset { 0% { opacity: 0; } 100% { opacity: 0; } }
@keyframes fade-in { 0% { opacity: 0; } 60% { opacity: 0; } 100% { opacity: 1; } }
@-webkit-keyframes reset { 0% { opacity: 0; } 100% { opacity: 0; } }
@-webkit-keyframes fade-in { 0% { opacity: 0; } 60% { opacity: 0; } 100% { opacity: 1; } }
@-moz-keyframes reset { 0% { opacity: 0; } 100% { opacity: 0; } }
@-moz-keyframes fade-in { 0% { opacity: 0; } 60% { opacity: 0; } 100% { opacity: 1; } }
@-o-keyframes reset { 0% { opacity: 0; } 100% { opacity: 0; } }
@-o-keyframes fade-in { 0% { opacity: 0; } 60% { opacity: 0; } 100% { opacity: 1; } }
.playlist {
animation-name: reset, fade-in;
animation-duration: 2.5s;
animation-timing-function: ease-in;
animation-iteration-count: 1;
animation-delay: 0, 0;
-webkit-animation-name: reset, fade-in;
-webkit-animation-duration: 2.5s;
-webkit-animation-timing-function: ease-in;
-webkit-animation-iteration-count: 1;
-webkit-animation-delay: 0, 0;
-moz-animation-name: reset, fade-in;
-moz-animation-duration: 2.5s;
-moz-animation-timing-function: ease-in;
-moz-animation-iteration-count: 1;
-moz-animation-delay: 0, 0;
-o-animation-name: reset, fade-in;
-o-animation-duration: 2.5s;
-o-animation-timing-function: ease-in;
-o-animation-iteration-count: 1;
-o-animation-delay: 0, 0;
}
fadeTo
与delay
一起使用,但如果您想按照自己的方式进行操作,请尝试以下操作:
function fadeInPlaylist() {
$(this).css('opacity',1);
}
$(window).load(function() {
$('.playlist').each(function(i, e) {
setTimeout(function(){ fadeInPlaylist.call(e); }, 1000 * i);
});
});
演示:http://jsbin.com/oyazof/1/edit
编辑:
如果你想用CSS3转换来做到这一点,你可以添加一个类,而不是从jQuery更改css。
j查询:
function fadeInPlaylist() {
$(this).addClass('opacity');
}
.CSS:
.opacity {
-webkit-transition: opacity .5s ease-in-out;
-moz-transition: opacity .5s ease-in-out;
-ms-transition: opacity .5s ease-in-out;
-o-transition: opacity .5s ease-in-out;
transition: opacity .5s ease-in-out;
opacity: 1;
}
使用 CSS3 过渡的演示:http://jsbin.com/oyazof/3/edit
只需更改:
elem.css('opacity',1);
自:
elem.fadeTo('fast', 1);