2011-06-16 79 views
0

我正在爲網頁製作一個自動動畫背景搖擺,而且我不知道如何解決當背景在最後回到原點。Swing效果jQuery動畫背景

這是我從其他網站做的代碼:

function scrollbackground() { 
     // decrease the offset by 1, or if its less than 1 increase it by the background height minus 1 
//   offset = (offset < 1) ? offset + (backgroundheight - 1) : offset - 1; 
     offset = offset - 1; 
     // apply the background position 
     $('body').css("background-position", "50% " + offset + "px"); 
     // call self to continue animation 
     setTimeout(function() { 
      scrollbackground(); 
      }, 1000 
     ); 
    } 

這都非常好,在一個方向移動。我要的是當背景到達一個特定的偏移,「重新長出」 0

回答

0

試試這個

var backgroundImageWidth; 
var moveLeft = true; 
var offset = 0; 
    function scrollbackground() { 

offset = moveLeft?offset-1:offset+1; 
if(offset*-1 == backgroundImageWidth) 
    moveLeft = false; 
else if(offset == 0) 
    moveLeft = true; 

      // apply the background position 
      $('body').css("background-position", "50% " + offset + "px"); 
      // call self to continue animation 
      setTimeout(function() { 
       scrollbackground(); 
       }, 1000 
      ); 
     } 
+0

謝謝!它工作正常! – JNestudi 2011-06-17 06:25:29

0

,你可以把它作爲一個參數

function scrollbackground(limit) { 
     // decrease the offset by 1, or if its less than 1 increase it by the background height minus 1 
//   offset = (offset < 1) ? offset + (backgroundheight - 1) : offset - 1; 
     offset = offset<limit?offset - 1:offset + 1; 
     // apply the background position 
     $('body').css("background-position", "50% " + offset + "px"); 
     // call self to continue animation 
     setTimeout(function() { 
      scrollbackground(limit); 
      }, 1000 
     ); 
    }