문제

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>
도움이 되었습니까?

해결책 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');

다른 팁

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]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top