1

我该怎么做?我只是出于性能原因尝试使用 css3(淡入可能会有点不稳定)。

现在它们都同时发生。

function fadeInPlaylist(elem) {
    elem.css('opacity',1);
}

$(window).load(function() {
   $('.playlist').each(function(i) {
      setTimeout(fadeInPlaylist($(this)),2500*i);
   });
});        
4

4 回答 4

3

你打错电话setTimeout了。

setTimeout(fadeInPlaylist($(this)),2500*i);

应该:

setTimeout(function(){fadeInPlaylist($(this));},2500*i);

此外,这是一个工作小提琴:http: //jsfiddle.net/q7Wa8/

于 2012-09-14T22:13:34.540 回答
1

如果您真的只需要使用 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;
}
于 2012-09-14T22:28:17.300 回答
0

您可以使用fadeTowithdelay但如果您想按照自己的方式进行操作,请尝试以下操作:

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。

jQuery:

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

于 2012-09-14T22:15:55.080 回答
-1

只是改变:

elem.css('opacity',1);

到:

elem.fadeTo('fast', 1);
于 2012-09-14T22:13:15.750 回答