简体   繁体   English

同一行上的JavaScript速记数学用于定义CSS值

[英]JavaScript shorthand math on same line for defining CSS value

What is the shorthand to assign a CSS style and add the pixels on the same line? 分配CSS样式并在同一行上添加像素的快捷方式是什么? The following doesn't work though it should be exceptionally clear what I'm trying to do: 以下内容不起作用,尽管应该格外清楚我要做什么:

header.style.top = (h + 100)+px;

px is not defined. px未定义。 I believe you are intending to concat a string. 我相信您打算连接一个字符串。 Observe the following... 注意以下几点...

header.style.top = (h + 100) + 'px';

JSFiddle Link - simplified demo JSFiddle Link-简化的演示

header.style.top = (h + 100) + 'px';

I believe you don't need to specify the px value. 我相信您不需要指定px值。 It defaults to pixels 默认为pixels

checkout this question: Using Javascript to add width to a HTML element 查看此问题: 使用Javascript向HTML元素添加宽度

As found here http://javascript.info/tutorial/styles-and-classes-getcomputedstyle they say to enclose the px in brackets 'px' . http://javascript.info/tutorial/styles-and-classes-getcomputedstyle所示,他们说将px括在方括号'px' Like the above answers. 像上面的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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