[英]Javascript random background image size
I have a code that shows a random background everytime I refresh, but I have no idea how I can make all images width100% and heigth 100%. 我有一个代码,每次刷新时都会显示随机背景,但是我不知道如何使所有图像的宽度分别为100%和高度100%。 Here's my code: 这是我的代码:
<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>
Any idea how to do this? 任何想法如何做到这一点?
You can try placing an image tag and setting the src property.That way , you can only specify the width and give an auto height. 您可以尝试放置图片标签并设置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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.