3

マウスの位置に合わせて回転する3D立方体を表示する小さなウィジェットを作成しています。ブラウザウィンドウを24の個別の水平セクションとして表示し、マウスがどのセクションに触れているかに基づいて適切な回転ステージを表示するif/elseを使用して効果を達成しました。ローカルではうまく機能しますが、オンラインで見るとパフォーマンスは完全に低下します。画像をプリロードすることで違いが出ると思いましたが、パフォーマンスには役立たないようです。私の推測では、画像はプリロードされていてもサーバーから呼び出されています。画像のサイズはそれぞれ約2kbです。何か案は?

完全なコードは以下のとおりです。

<!DOCTYPE html>
<head>
<title> Move your mouse, rotate the cube</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script language="JavaScript">    
      function rackEmUp()   {
      var i = 0;

      imageObj = new Image();

      images = new Array();
      images[0]="img/1.png"
      images[1]="img/2.png"
      images[2]="img/3.png"
      images[3]="img/4.png"
      images[4]="img/5.png"
      images[5]="img/6.png"
      images[6]="img/7.png"
      images[7]="img/8.png"
      images[8]="img/9.png"
      images[9]="img/10.png"
      images[10]="img/11.png"
      images[11]="img/12.png"
      images[12]="img/13.png"
      images[13]="img/14.png"
      images[14]="img/15.png"
      images[15]="img/16.png"
      images[16]="img/17.png"
      images[17]="img/18.png"
      images[18]="img/19.png"
      images[19]="img/20.png"
      images[20]="img/21.png"
      images[21]="img/22.png"
      images[22]="img/23.png"
      images[23]="img/24.png"

      for(i=0; i<=23; i++) {
           imageObj.src=images[i];
      }    
                                  }     
    rackEmUp();  </script>
</head>
<body>

 <img class="boxStage" src="img/1.png">

 <script type="text/javascript">

 $(document).mousemove(function(event){
     var mloc = {
         x: event.pageX
     };

     if( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.04)
     ){
         $(".boxStage").attr("src", images[0]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.08)
     ){
         $(".boxStage").attr("src", images[1]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.12)
     ){
         $(".boxStage").attr("src", images[2]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.16)
     ){
         $(".boxStage").attr("src", images[3]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.20)
     ){
         $(".boxStage").attr("src", images[4]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.24)
     ){
         $(".boxStage").attr("src", images[5]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.28)
     ){
         $(".boxStage").attr("src", images[6]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.32)
     ){
         $(".boxStage").attr("src", images[7]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.36)
     ){
         $(".boxStage").attr("src", images[8]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.40)
     ){
         $(".boxStage").attr("src", images[9]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.44)
     ){
         $(".boxStage").attr("src", images[10]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.48)
     ){
         $(".boxStage").attr("src", images[11]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.52)
     ){
         $(".boxStage").attr("src", images[12]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.56)
     ){
         $(".boxStage").attr("src", images[13]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.60)
     ){
         $(".boxStage").attr("src", images[14]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.64)
     ){
         $(".boxStage").attr("src", images[15]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.68)
     ){
         $(".boxStage").attr("src", images[16]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.72)
     ){
         $(".boxStage").attr("src", images[17]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.76)
     ){
         $(".boxStage").attr("src", images[18]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.80)
     ){
         $(".boxStage").attr("src", images[19]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.84)
     ){
         $(".boxStage").attr("src", images[20]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.88)
     ){
         $(".boxStage").attr("src", images[21]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.92)
     ){
         $(".boxStage").attr("src", images[22]);

     } else if

     ( 
         (mloc.x >= 0 && mloc.x <= $(document).width()*0.96)
     ){
         $(".boxStage").attr("src", images[23]);

     } else

     {
         $(".boxStage").attr("src", images[0]);
     };
        }); </script> </body> </html>

EDIT: Thanks everybody for your help - I wound up making one large banner image and changing the CSS background-position property in accordance with the position of the mouse.

4

4 に答える 4

4

各画像を個別にロードする代わりに、1 つの大きな画像スプライトを作成し、CSS 背景位置を変更します。24 個の画像とリクエストではなく、1 つの画像と 1 つの HTTP リクエストで作業するため、これによりパフォーマンスが向上するはずです。

これは便利です: http://spritely.net/特に、より複雑なアニメーションを実行したい場合。

スプライトを使用したアニメーションの例を次に示します: http://cloud.github.com/downloads/Dhirajkumar/DG_Slider/animated-backgound-image-sprite.htm

于 2012-11-07T15:51:29.657 に答える
2

私ならこうします、

両方の長所 (イメージ インデックスの計算に関する Shmiddty の提案のおかげ):

var images = [],
    $stage = $('.stage'),
    oldIndex;

function preload() {
    for (i = 0; i < preload.arguments.length; i++) {
        images[i] = new Image();
        images[i].src = preload.arguments[i];
    }
}

preload(
   "http://draves.org/pix/kdn/gen/acanthametra/big%20two%20spike%20star%20copy.small.jpg",
   "http://draves.org/pix/kdn/gen/ascidiae/orange%20ten%20branch%20copy.small.jpg", 
   "http://draves.org/pix/kdn/gen/ascidiae/six%20star%20copy.small.jpg");

$(document).mousemove(function(event) {
    var mloc       = { x: event.pageX },
        imageIndex = Math.floor(mloc.x / $(document).width() * images.length);

    if (oldIndex !== 'undefined' && oldIndex !== imageIndex) {

        oldIndex = imageIndex;
        $stage.css('background-image', 'url(' + images[imageIndex].src + ')');
    }
});​

JSFiddle
http://jsfiddle.net/rzY5L/2/

これらのプリローダーの詳細は、ここで見つけることができます (ただし、一部は古くなっています):
http://perishablepress.com/3-ways-preload-images-css-javascript-ajax/

更新
点滅する画像は、.png の処理に関連しているようです (少なくとも Chrome では、画像を変更すると点滅するようです)。1 つの解決策は、これらのイメージの base64 バージョンを作成することです。コンバーターはここにあります:
http://www.greywyvern.com/code/php/binary2base64

Base64 Fiddle (点滅なし)
http://jsfiddle.net/rzY5L/3/

于 2012-11-07T16:26:28.770 に答える
1

私が使用する実用的なコードサンプル:

// preload images
var images = new Array()
function preload() {
    for (i = 0; i < preload.arguments.length; i++) {
        images[i] = new Image()
        images[i].src = location.origin + "/PaymentManager" + preload.arguments[i]
    }
}
preload("/res/cards/visa.png","/res/cards/mastercard.png","/res/cards/amex.png","/images/loader.png");

すべての画像に対して新しいImageオブジェクトを作成する必要があるようです。

于 2012-11-07T16:08:02.157 に答える
1

w3Schoolsより: 「HTML ドキュメントのタグごとに、Image オブジェクトが作成されます。」

このリンクをご利用いただきありがとうございます。しかし、私には、画像オブジェクトが1つしかないようです。new Image()事前にロードしようとしているすべての画像を作成してみてください。

var image1 = new Image(); 
image1.src= 'img/1.png'; 

var image2 = new Image(); 
image2.src = 'img/2.png' 

...

別の便利なリンクを次に示します。

于 2012-11-07T16:03:30.040 に答える