3

ページ内をランダムに移動する div があります。

古い質問

divを「矢印」のような画像に置き換えます。これで、ランダムに移動し、正しい方向を指し(移動時に回転角度を変更します)、常に上を指すとは限りません。

矢印

移動時に角度を回転させると思いますが、古い点と新しい点の間の角度を計算する方法がわかりません。

前もって感謝します!!!

ここに画像の説明を入力

4

1 に答える 1

3

これを試して

$(document).ready(function(){
    animateIMG();

});

function makeNewPosition(){

    // Get viewport dimensions (remove the dimension of the div)
    var h = $(window).height() - 50;
    var w = $(window).width() - 50;

    var nh = Math.floor(Math.random() * h);
    var nw = Math.floor(Math.random() * w);

    return [nh,nw];    

}

function animateIMG(){
    var newq = makeNewPosition();
    $('img').animate({ top: newq[0], left: newq[1] }, function(){
      animateIMG();        
    });

};

速度制御付きのJsFiddle http://jsfiddle.net/D6Svc/

于 2012-11-12T08:39:32.067 に答える