使背景位置变量

时间:2011-10-03 03:41:22

标签: javascript

我正在使用此代码...

document.getElementById('a1').style.backgroundPosition = '0px 0px';

并且它工作正常,但无论如何使用javascript制作定位变量?

像这样......

document.getElementById('a1').style.backgroundPosition = '0px VariableHere';

2 个答案:

答案 0 :(得分:7)

由于背景位置是一个字符串,您只需将您的值连接起来。

例如:

var yValue = 20;
document.getElementById('a1').style.backgroundPosition = '0px ' + yValue + 'px';

答案 1 :(得分:0)

var bgPos = '20px';
document.getElementById('a1').style.backgroundPosition = '0px ' + bgPos;