fadeIn()然后使用jQuery同时动画(多个id)

时间:2015-04-09 13:20:15

标签: jquery animation fadein

我不能再思考了。如果有人能帮助我解决这个问题,我将不胜感激。比方说,我有6个立方体,我想逐个淡入屏幕,然后同时展开。我正在使用fadeInanimate方法。这是我的代码,

HTML

<div id="parentBox">
        <svg id="polyWhite" width="200" height="270" viewBox="0 0 512 606">
            <g>
                <polygon points="256,343 47,195 256,55 465,195" class="top-white" />
                <polygon points="256,343 465,195 465,480 256,627" class="right-white" />
                <polygon points="256,343 256,627 47,480 47,195" class="left-white" />
            </g>
        </svg>
        <svg id="polyBlack" width="200" height="270" viewBox="0 0 512 606">
            <g>
                <polygon points="256,343 47,195 256,55 465,195" class="top-black" />
                <polygon points="256,343 465,195 465,480 256,627" class="right-black" />
                <polygon points="256,343 256,627 47,480 47,195" class="left-black" />
            </g>
        </svg>
        <svg id="polyRedTwo" width="200" height="270" viewBox="0 0 512 606">
            <g>
                <polygon points="256,343 47,195 256,55 465,195" class="top-red-two" />
                <polygon points="256,343 465,195 465,480 256,627" class="right-red-two" />
                <polygon points="256,343 256,627 47,480 47,195" class="left-red-two" />
            </g>
        </svg>
        <svg id="polyBlackTwo" width="200" height="270" viewBox="0 0 512 606">
            <g>
                <polygon points="256,343 47,195 256,55 465,195" class="top-black-two" />
                <polygon points="256,343 465,195 465,480 256,627" class="right-black-two" />
                <polygon points="256,343 256,627 47,480 47,195" class="left-black-two" />
            </g>
        </svg>
        <svg id="polyRedThree" width="200" height="270" viewBox="0 0 512 606">
            <g>
                <polygon points="256,343 47,195 256,55 465,195" class="top-red-three" />
                <polygon points="256,343 465,195 465,480 256,627" class="right-red-three" />
                <polygon points="256,343 256,627 47,480 47,195" class="left-red-three" />
            </g>
        </svg>
        <svg id="polyBlackThree" width="200" height="270" viewBox="0 0 512 606">
            <g>
                <polygon points="256,343 47,195 256,55 465,195" class="top-black-three" />
                <polygon points="256,343 465,195 465,480 256,627" class="right-black-three" />
                <polygon points="256,343 256,627 47,480 47,195" class="left-black-three" />
            </g>
        </svg>
        <svg id="polyRed" width="200" height="270" viewBox="0 0 512 606">
            <g>
                <polygon points="256,343 47,195 256,55 465,195" class="top" />
                <polygon points="256,343 465,195 465,480 256,627" class="right" />
                <polygon points="256,343 256,627 47,480 47,195" class="left" />
            </g>
        </svg>
    </div>

jQuery

$("svg").each(function(index) {
        $(this).delay(400 * index).fadeIn(300);
    });
    $("#polyRed").delay(2000).stop().animate({
        left: "-23",
        top: "9"
    });
    $("#polyBlack").delay(2000).stop().animate({
        left: "140",
        top: "-102"
    });
    $("#polyBlackThree").delay(2000).stop().animate({
        left: "-22",
        top: "233"
    });
    $("#polyBlackTwo").delay(2000).stop().animate({
        left: "304",
        top: "233"
    });
    $("#polyRedTwo").delay(2000).stop().animate({
        left: "305",
        top: "10"
    });
    $("#polyRedThree").delay(2000).stop().animate({
        left: "140",
        top: "345"
    });  

我同时获得动画,但后来我失去了fadeIn的延迟。一些jQuery专家可以帮助我解决出错的问题。

2 个答案:

答案 0 :(得分:1)

它们在执行中是异步的。您必须在上一个元素的动画结束时调用下一个动画(通过不透明度与使用fadeIn相同)。

这对我有帮助

var allSVGs = $("svg");
fadeIn(0);

....

function fadeIn(svgIndex){
   allSVGs.eq(svgIndex).animate({"opacity":"1"}, {
   complete:function(){
      svgIndex++;
      if(svgIndex < allSVGs.length) //Making sure we don't exceed the maximum available SVG elements
         fadeIn(svgIndex); //Recursively calling the next elements animation (hide) from the completed one.
   }});
}

修改jsFiddle Link。我只是评论了代码的其他方面,使事情变得更容易......

答案 1 :(得分:1)

尝试

$("#parentBox").queue("boxes", $.map($("#parentBox svg"), function(el, index) {
    return function(next) {
        $(el).delay(400 * index).fadeTo(1000, 1, next)
    }
})).dequeue("boxes").promise("boxes")
.then(function() {
  $("#polyRed").delay(2000).stop().animate({
    left: "-23",
    top: "9"
  });
  $("#polyBlack").delay(2000).stop().animate({
    left: "140",
    top: "-102"
  });
  $("#polyBlackThree").delay(2000).stop().animate({
    left: "-22",
    top: "233"
  });
  $("#polyBlackTwo").delay(2000).stop().animate({
    left: "304",
    top: "233"
  });
  $("#polyRedTwo").delay(2000).stop().animate({
    left: "305",
    top: "10"
  });
  $("#polyRedThree").delay(2000).stop().animate({
    left: "140",
    top: "345"
  });
});

jsfiddle https://jsfiddle.net/ame17p3b/6/