简体   繁体   English

Jquery从上到下依次淡出

[英]Jquery fade in order from top to bottom

JS: JS:

$(function(){
 chainAnim('.section','slow','1')  });
 function chainAnim(e,s,o) {
        var $fade = $(e);
        var code = "console.log('Done.');";
        $fade.each(function(){
   code = "$('#"+$(this).attr('id')+"').fadeTo('"+s+"','"+o+"',function(){"+code+"});";
        });
        eval(code);
}

HTML: HTML:

 <div id="wrapper">
  <div class="section" id="section-1">Section 1</div>
  <div class="section" id="section-2">Section 2</div>
  <div class="section" id="section-3">Section 3</div>
  <div class="section" id="section-4">Section 4</div>
 </div>

When animating,section 4 is animated in first. 动画时,第4部分首先进行动画处理。 How can I reverse this? 我该怎么扭转呢?

This should do what you want, but I got rid of your eval() code. 这应该做你想要的,但我摆脱了你的eval()代码。 Not sure why you were taking that approach. 不知道你为什么采取这种方法。

Example: http://jsfiddle.net/wqWE5/ 示例: http //jsfiddle.net/wqWE5/

I also changed the second argument from "slow" to 800 so it could be used in the .delay() . 我还将第二个参数从"slow"更改为800因此可以在.delay()使用它。

The duration you pass multiplied by the current index of the .each() will make the animation happen in sequence. 您传递的持续时间乘以.each()的当前索引将使动画按顺序发生。

$(function(){
     chainAnim('.section',800,'1');
});

function chainAnim(e,s,o) {
        var $fade = $(e);
        var code = function() {console.log('Done.');};
        $fade.each(function( i ){
            $(this).delay(i * s).fadeTo(s,o,code);
        });
} 

Why not just offset the fadeIn() with a delay() 为什么不用延迟()来抵消fadeIn()

$('#wrapper .section').each(function(i){
    $(this).delay(i*site.rate).fadeIn(site.rate);
});

To reverse them just do 要扭转它们就行了

$('#wrapper .section').each(function(i){
  var c = $('#wrapper .section').length;
  $(this).delay( (c-i) *site.rate).fadeIn(site.rate);
});

Of course section 4 is animated first, because "code" is set to the last one in the loop ;) 当然第4部分首先是动画,因为“代码”被设置为循环中的最后一个;)

Code gets overwritten in every loop cycle, you should use += instead of = in the loop. 代码在每个循环周期中被覆盖,你应该在循环中使用+ =而不是=。

Anyway, your approach isn't best practice, check this out: 无论如何,你的方法不是最佳做法,请查看:

Edit: Recursive Fade! 编辑:递归淡化!

$(document).ready(function() {
 chainAnim($('.section'), 'slow', 1);
});

function chainAnim(e, s, o) {
  e = $.makeArray(e);
  if(e.length == 0) { return; }
  $(e.shift()).fadeTo(s, o, function() {
    console.log('Done.');
    chainAnim(e, s, o);
  });
}

Demo: http://jsfiddle.net/97dEc/3/ 演示: http//jsfiddle.net/97dEc/3/

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

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