添加javascript像素值? [英] Add javascript pixel values?
本文介绍了添加javascript像素值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
javascript / jQuery中是否有一种方法可以将值为60px和40px的两个变量加在一起得到100px?
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 trying to calculate positions of objects relative to other objects and it would be really convenient if I could do something like this:
$('#object2').css('left', $('#object1').css('left')+'60px');
当然,这只是无效的40px60px。
Of course, that just gives in invalid "40px60px".
推荐答案
从字符串中删除px,添加值,然后再添加px
remove px from strings, add values, then add px back
(parseInt("40px".replace(/px/,""))+60)+"px"
这篇关于添加javascript像素值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文