2013-01-24 125 views
4

我有一個javascript計算的小問題。我有這樣的功能:小計算問題

$("#main-nav li[class]").each(function(index) { 
    var position = $(this).offset(); 
    var width = $(this).width(); 
    var center = parseInt(position) - (parseInt(width)/2); 
    console.log("Position: " + position.left + ", width: " + width + ", center: " + center); 
}); 

但它導致了這一點。任何人都有一個想法如何計算沒有完成?

Position: 722, width: 83, center: NaN 

回答

7

position.left

$("#main-nav li[class]").each(function(index) { 
    var position = $(this).offset(); 
    var width = $(this).width(); 
    var center = parseInt(position.left) - (parseInt(width)/2); 
    console.log("Position: " + position.left + ", width: " + width + ", center: " + center); 
}); 
+1

總是最小的錯誤,謝謝! – Veltar

+0

發生錯誤。沒有問題! :) –

1

只是要簡單的修正此線路上。然後你會得到所需的輸出..

var center = parseInt(position.left) - (parseInt(width)/2); 
+0

@Velter只是一個簡單的錯誤沒有別的.. :-) – Codegiant