gravityboy
gravityboy

Reputation: 817

Make background position variable

I'm using this code...

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

and it works fine but is there anyway to make the positioning variable with javascript?

like so...

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

Upvotes: 2

Views: 8830

Answers (2)

SeanCannon
SeanCannon

Reputation: 78006

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

Upvotes: 0

Jon Newmuis
Jon Newmuis

Reputation: 26530

Since the background position is a string, you can just concatenate your value in.

For example:

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

Upvotes: 7

Related Questions