2011-04-17 16 views
0

我有一個問題。我將這張3060像素的圖像作爲我的背景圖像,當我按菜單鏈接時,我想將背景向右或向左「滑動」,但是當我這樣做時,我只能使用X如果我使用backgroundPositionX,它不會做任何事情。但是,當我使用backgroundPosition時,我只能提供1個參數。我只想做一些事情到X方面而不是Y.jQuery backgroundPostion和動畫不僅可以獲得Y

我現在的代碼將Y自動設置爲50%,這不是想要的。

 function move(space) { 
      image = 3060; 
      bodyWidth = $('body').width(); 
      offset = 900 * space; 
      if(image - offset < bodyWidth) 
       offset = image - bodyWidth - 15; 

      console.log(offset); 
      console.log(bodyWidth); 


      $('body').animate({ 
       backgroundPosition: -offset 


      }, 5000, function() { 
       console.log("animate complete"); 
      }); 

      //$('body').css('backgroundPosition', '-'+offset+'px 0px'); 
     } 

回答

0

您需要放置x和y座標才能使其工作。例如: 說你想移動從0像素的背景位置500像素X上

$(somediv).animate({backgroundPosition: '500px 0px'}) 

假設背景原始位置0像素* 0像素,這將移動背景X:500像素和Y:0像素。然後你可以像這樣把它帶回來:

.animate({backgroundPosition: '0px 0px'}) 
+0

即使我backgroundPosition:「500px 50px」動畫dosn't工作。只有當我backgroundPosition:偏移量或只給出1個數字它的作品。 – Niclas 2011-04-17 20:20:25

+0

http://snook.ca/technical/jquery-bg/看看這可能會幫助你 – Bene 2011-04-17 20:54:06