2015-05-14 69 views
4

我正在通過CodeCademy上的jQuery課程開展工作,此特定任務涉及將開關函數與'.animate()'函數的多個實例結合使用。有問題的'img'是我們最喜歡的意大利水管工,但他只會左轉。當我運行代碼時,我得到這個錯誤返回「 糟糕,再試一次。當你按'右'時,你的圖像看起來不能右移。」使用'.animate()'函數發佈功能

$(document).ready(function() { 
    $(document).keydown(function(key) { 
     switch(parseInt(key.which,10)) { 
      // Left arrow key pressed 
      case 37: 
       $('img').animate({left: "-=10px"}, 'fast'); 
       break; 
      // Up Arrow Pressed 
      case 38: 
       $('img').animate({up: "-=10px"}, 'fast'); 
       break; 
      // Right Arrow Pressed 
      case 39: 
      $('img').animate({right: "-=10px"}, 'fast'); 
       break; 
      // Down Arrow Pressed 
      case 40: 
       $('img').animate({down: "-=10px"}, 'fast'); 
       break; 
     } 
    }); 
}); 

回答

2
$(document).ready(function() { 
    $(document).keydown(function (key) { 
     switch (parseInt(key.which, 10)) { 
      // Left arrow key pressed 
      case 37: 
       $('img').animate({left: "-=10px"}, 'fast'); 
       break; 
       // Up Arrow Pressed 
      case 38: 
       $('img').animate({top: "-=10px"}, 'fast'); 
       break; 
       // Right Arrow Pressed 
      case 39: 
       $('img').animate({left: "+=10px"}, 'fast'); 
       break; 
       // Down Array Pressed 
      case 40: 
       $('img').animate({top: "+=10px"}, 'fast'); 
       break; 
     } 
    }); 
}); 

嘗試使用左邊和頂部,而不是否則你會被CSS惡魔在作祟。謝謝你,這個工程,但不要倒轉! :)