Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

small calculation issue [closed]

I have a small problem with a javascript calculation. I have a function like this:

$( "#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);
});   

But it results in this. Anyone have an idea how the calculation isn't done?

Position: 722, width: 83, center: NaN 
like image 798
Veltar Avatar asked Feb 18 '23 18:02

Veltar


1 Answers

Give 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);
}); 
like image 107
Praveen Kumar Purushothaman Avatar answered Feb 20 '23 09:02

Praveen Kumar Purushothaman