简体   繁体   中英

incremental css3 fadein

How might I do this? I'm trying to use css3 only for performance reasons (fade in can get kind of choppy).

Right now they all happen at the same time.

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

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

You are calling setTimeout incorrectly.

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

should be:

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

Also, here's a working fiddle: http://jsfiddle.net/q7Wa8/

If you really need to do it with CSS3 only, use this code:

@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;
}

But you'll have compatibility issues. Here's the cross-browser code, doesn't work in 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;
}

You could use fadeTo with delay but if you want to do it your way try this:

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

$(window).load(function() {
   $('.playlist').each(function(i, e) {
     setTimeout(function(){ fadeInPlaylist.call(e); }, 1000 * i);
   });
});

Demo: http://jsbin.com/oyazof/1/edit

Edit:

If you want to do it with CSS3 transitions you can just add a class instead of changing the css from jQuery.

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

Demo with CSS3 transitions: http://jsbin.com/oyazof/3/edit

Just change:

elem.css('opacity',1);

To:

elem.fadeTo('fast', 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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