2

4 つの画像 (logo-1、logo-2、logo-3、logo-4) があり、ボタンをクリックしたときにそれらを次々に表示したいと考えています。

まず、画像の高さを 0 に設定して、easyOutBounce 効果で表示します。

アニメーション機能を使用していますが、4つの画像が同時に表示されます。

アニメーションを連結するにはどうすればよいですか?

<script>
function mostrar(num_logo) {
    if (num_logo <= 4) {
        $("#logo-"+num_logo)    
            .stop().animate(
                {height: '218px'},
                {queue:false, 
                 duration:1000, 
                 easing: 'easeOutBounce',
                 complete: mostrar(num_logo+1)}                             
            );

    }
}
    $(function() {
        // run the currently selected effect
        function runEffect() {

            for (i=1;i<=4;i++) {
                $("#logo-"+i).css('height', '0px');
            }   
            mostrar(1);


        };

        $( "#button" ).click(function() {
            runEffect();
            return false;
        });

    });
</script>
4

2 に答える 2

2

ジャスパーからの答えは良いです。私はあなたが読むべきいくつかのリンクをあなたに与えます:それらはあなたがあなたのコードで犯したいくつかの一般的な間違いとそれらを避ける方法を説明します;)

于 2011-11-29T20:48:47.410 に答える
2

これが私が非常によく似た質問に答えるために作ったjsfiddleです:http://jsfiddle.net/jJ8vJ/ 質問はここにあります:順序で位置をアニメーション化する方法?)。

基本的な考え方は、jQueryオブジェクト(順番にアニメーション化する要素)の配列を設定し、.animate()関数のコールバックを使用して要素を順番にアニメーション化することです。

var $items     = [$('#one'), $('#two'), $('#three'), $('#four')],//stores the DOM elements to be animated in order
    item_index = 0,//stores the current index in the animation queue
    animations = {
        '0px0px'     : { left : '100px', top : '0px' },//0px0px means the element is at the top left
        '100px0px'   : { left : '100px', top : '100px' },//100px0px means the element is at the top right
        '0px100px'   : { left : '0px'  , top : '0px' },//0px100px means the element is at the bottom left
        '100px100px' : { left : '0px'  , top : '100px' }//100px100px means the element is at the bottom right
    };//this stores the conversion between where the element is and where it should animate to

//setup a function that can call itself inside the animation callback to iterate through the DOM elements to be animated
function run_animation($element) {

    //check to make sure there are more DOM elements to animate, if none are found then the process is done
    if (item_index in $items) {

        //get this element's top and left CSS properties and put them together so we can convert that to the next coordinates it needs
        var css = $items[item_index].css('left') + '' + $items[item_index].css('top');

        //now animate this element by converting the current top/left CSS properties to its next coordinates
        $items[item_index].animate({
            left : animations[css].left,
            top  : animations[css].top
        }, 1500, function () {

            //here we run this same function for the next index
            item_index++;
            run_animation($items[item_index]);
        });
    }
}

//run the animation function for the first time
run_animation($items[item_index]);
于 2011-11-29T20:26:28.610 に答える