2013-10-13 2 views
2

나는 새로 고침 할 때마다 임의의 배경을 보여주는 코드를 가지고 있지만, 모든 이미지를 width100 %와 heigth 100 % 만들 수있는 방법을 모릅니다. 내 코드는 다음과 같습니다.자바 스크립트 무작위 배경 이미지 크기

<html> 
<head> 
<script type="text/javascript"> 
var totalCount = 3; 
function ChangeIt() 
{ 
var num = Math.ceil(Math.random() * totalCount); 
document.body.background = 'bg'+num+'.png'; 
document.body.style.backgroundRepeat = "repeat";// bg repeat. 
} 
</script> 
</head> 
<body> 
---- 
</body> 
<script type="text/javascript"> 
ChangeIt(); 
</script> 
</html> 

아이디어가 있습니까?

+0

나는 실제로 당신의 요점을 얻지 못합니다. – yakiang

답변

0

이미지 태그를 삽입하고 src 속성을 설정해 볼 수 있습니다. 이렇게하면 너비 만 지정하고 자동 높이를 지정할 수 있습니다.

function ChangeIt() 
{ 
    var widthOfDoc = document.body.clientWidth; 
    var num = Math.ceil(Math.random() * totalCount); 
    var image = new Image(); 
    image.src = 'bg'+num+'.png'; 
    image.width = widthOfDoc; 
    document.body.appendChild(image); 
}