2013-09-16 17 views
0

下面的代碼遍歷精靈圖像,運動-120每秒,但由於某些原因,它是跳躍從0到-120,然後以120改爲-240jQuery的:數學失敗

thismarginLeft = $this.css("margin-left").replace(/[^0-9]/g,''); 
    if(thismarginLeft < -360 || thismarginLeft == 0){ 
      thismarginLeft = thismarginLeft - 120; 
      if(thismarginLeft < -360){ 
       thismarginLeft = 0; 
      } 
    } 

http://jsfiddle.net/FDRmv/1/

+0

請在您的問題所有相關的代碼。 – forivall

回答

3

由於yahermann說,問題就來了從替換方法中刪除您的margin-left css前面的' - '。

你甚至可以使用「parseInt函數」如果你不想使用「替換」:(這裏的例子:http://jsfiddle.net/FDRmv/2/

var internalMiniPoster; 
$(".miniPosterImg").hover(function() { 
    var $this = $(this); 
    internalMiniPoster = setInterval(function() { 
     thismarginLeft = parseInt($this.css("margin-left"),10); // always use a radix 
     if (thismarginLeft < -360 || thismarginLeft == 0) { 
      thismarginLeft = thismarginLeft - 120; 
      if (thismarginLeft < -360) { 
       thismarginLeft = 0; 
      } 
     } 
     $this.css("margin-left", thismarginLeft + "px"); 
    }, 1000); 
}, function() { 
    clearInterval(internalMiniPoster); 
}); 
1

很難弄清楚你在做什麼。

但也許你的意思是這樣的:

if(thismarginLeft > -360){ 

,而不是這樣的:

if(thismarginLeft < -360 || thismarginLeft == 0){ 

還包括 - 在您的正則表達式:

thismarginLeft = $this.css("margin-left").replace(/[^0-9\-]/g,'');