2013-01-24 199 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