2

無期限にパルスインおよびパルスアウトしたい単純なフェードインがあります。これを行うプラグインを見つけましたが、jquery にすでに loop() API があるかどうかに興味があったので、スクリプトでそれを処理することができました。

<script type="text/javascript">
$(document).ready(function(){    
    $('.bottom-left').delay(1000).fadeIn(900);
    $('.bottom-right').delay(3000).fadeIn(700);
});
</script>
4

1 に答える 1

7

複雑にしたい場合、これは大量のコードになる可能性がありますが、単純な実装は数行で済みます。基本的に、アニメーション関数のコールバック関数から、要素を非表示または表示する関数を再帰的に呼び出したいとします。

$(function () {

    //declare a function that can fade in/out any element with a specified delay and duration
    function run_animation($element, delay, duration) {

        //animate fade in/out after delay
        $element.delay(delay).fadeToggle(duration, function () {

            //after fade in/out is done, recursively call this function again with the same information
            //(if faded-out this time then next-time it will be faded-in)
            run_animation($element, delay, duration);
        });
    }

    //initialize the animations for each element, specifying a delay and duration as well
    run_animation($('.bottom-left'), 1000, 900);
    run_animation($('.bottom-right'), 3000, 700);
});

ここにデモがあります:http://jsfiddle.net/xpw4D/

のドキュメント.fadeToggle(): http://api.jquery.com/fadeToggle

アップデート

このコードを少し強化して、次のように 2 つ以上のステップをアニメーション化できます。

$(function () {

    function run_animation(options) {

        //initialize the count variable if this is the first time running and reset it to zero if there are no more steps
        if (typeof options.count == 'undefined' || options.count >= options.steps.length) {
            options.count = 0;
        }

        options.element.delay(options.steps[options.count].delay).fadeToggle(options.steps[options.count].duration, function () {

            options.count++;

            run_animation(options);
        });
    }

    run_animation({
        element  : $('.bottom-left'),
        steps    : [
            { delay : 1000, duration : 100 },
            { delay : 500, duration : 900 },
            { delay : 3000, duration : 500 }
        ]
    });
    run_animation({
        element  : $('.bottom-right'),
        steps    : [
            { delay : 2000, duration : 200 },
            { delay : 1000, duration : 1800 },
            { delay : 6000, duration : 1000 }
        ]
    });
});​

ここにデモがあります:http://jsfiddle.net/jasper/xpw4D/2/

于 2012-02-23T23:06:30.323 に答える