简体   繁体   中英

How do I subtract pixels from numbers in JavaScript?

I need to change the height of the footer by finding out the height of another div.

I'm using the .css("height") function, but I can't get the function to return the answer in pixels.

Here's the code I'm using. It's returning NaN .

if($("#middleContainer").height() > 500){
  var add_height = $("#middleContainer").css("height") - 500;
  alert(add_height);
}

After looking at the suggestions, I'm using the following code. It doesn't make any difference.

if($("#middleContainer").height() > 500){
  var add_height = $("#middleContainer").height() - 500;
  var new_height = -850 + add_height;
  alert(new_height);
  $(".footer").css("top", "new_height");
}

Following code return height with string type so you have to parse as Int

var add_height = parseInt($("#middleContainer").css("height")) - 500;
// Return as String data type value

This code return number so do not want to parse as Int it directly works

var add_height = $("#middleContainer").height();
// Return as Number data type value

这应该工作:

parseInt( $("#middleContainer").css("height"));

The answer is always in pixeis, you just need to parse it

var h =$(something).height() - 500;
$('something').height(h);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM