Add javascript pixel values?

前端 未结 5 480
甜味超标
甜味超标 2021-02-05 14:45

Is there a way in javascript/jQuery to take two variables with values \"60px\" and \"40px\" and add them together to get \"100px\"?

Or in a more general sense, I\'m tryi

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 15:06

    You're looking for the parseInt() function.

    var left1 = "40px";
    var left2 = "60px";
    
    // Add the integer values of the left values together
    var leftTotal = parseInt( left1, 10 ) + parseInt( left2, 10 ) + "px";
    

    Also worth investigating is the parseFloat() method. It does the same thing as parseInt(), but works on numbers with decimals (aka floating point values).

提交回复
热议问题