1

マウスをオブジェクトの上に置いたときに再生したいスプライトアニメーションがあり、マウスがオブジェクトから離れるたびに逆方向に再生して最初のフレームで停止します。

これは私がマウス入力アニメーションのために持っているものです:

$("#banner").mouseenter(function(){ $('#bannerstache').sprite({fps: 200, no_of_frames: 34,   play_frames:34}); });

基本的に、バナーにカーソルを合わせると、フレーム34で完了するまで口ひげが「成長」します(ただし、フレーム1では口ひげはありません)。

この口ひげを逆再生して、マウスがホバー状態を離れるたびにフレーム1に戻るようにします。

問題は、再生を逆にするいくつかの方法を試しましたが、常に最初のフレームに戻るとは限らないことです。

どうすればこれを達成できますか?

ご意見をお寄せいただき、誠にありがとうございます。

4

1 に答える 1

2

ここに私があなたの問題の説明から作り上げたものがあります

HTML:

<div id="div1" class="testContainer"></div>

CSS:

.testContainer {height:100px;width:150px;background-color:#CCC;text-align:center;font-size:90px}​

JavaScript:

// arrSprite would be an array of images
// each index represents a frame in your sprite
// for the purposes of this test i'll create an array of integers
var arrSprite1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// $div1 is a container within which we'll place our animation
var $div1 = $('#div1');

// create a new SpritePlayer() to handle our animation
var player = new SpritePlayer($div1, arrSprite1, 100);

// set the hover in/out event handler of our container.
$div1.hover(player.forward, player.reverse);

function SpritePlayer($container, arrSprite, speed) {
    var int, cur = 0, min = 0, max = arrSprite.length;

    // give $container the first frame in our sprite (arrSprite)
    $container.html(getFrame(min));

    return {
        forward: function () { playSprite(true); },
        reverse: function () { playSprite(false); }
    };

    function getFrame(i) { return arrSprite[i]; }

    function playSprite(forward) {
        var limit = forward ? max : min;

        clearInterval(int); int = setInterval(function() {
            $container.html(getFrame(forward ? ++cur : --cur));
            cur === limit && clearInterval(int); 
        }, speed);
    }
};
​

ライブデモ

于 2012-12-12T11:21:17.077 に答える