Question

I read here that I can position my div background image using:

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

But how would I do this for the body background-position using JavaScript, does the body take an id = 'bodyname' for the getElementById() function parameter?

<!DOCTYPE html>
<html>
<head>
<style>
body
{ 
    background-image: url('image.gif');
    background-repeat: repeat;
    background-attachment:fixed;
    background-position: 77px 100px;
}
</style>
</head>

<body>

</body>
</html>
Était-ce utile?

La solution 2

In order to select your body with getElementById you need to add an id to it:

html:

<body id="bodyid"></body>

javascript:

var body = document.getElementById('bodyid');

Autres conseils

You could give the body an id, there is no problem with that. But you could just as easily get the element by the tagname.

var bodyElements = document.getElementsByTagName('body');

As that method returns an array, the body tag would be the first element:

bodyElements[0]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top