admin管理员组文章数量:1426810
I have this code which randomly shows a background image. I sized up the images and lower the bytes to lower the loading speed. However, I want the image to resize with whatever the screen size is.
<script type="text/javascript">
var randnum = Math.random();
var inum = 3;
var rand1 = Math.round(randnum * (inum - 1)) + 1;
images = new Array();
images[1] = "bgImages/Blue_Venetian_Glass.jpg";
images[2] = "bgImages/Etched Glass.jpg";
images[3] = "bgImages/glass.jpg";
images[4] = "bgImages/Raindrops_on_Glass.jpg";
var image = images[rand1];
document.body.style.background = 'url("' + image + '") no-repeat center center fixed';
</script>
My question is "what do I need to make the image stretch"
I have this code which randomly shows a background image. I sized up the images and lower the bytes to lower the loading speed. However, I want the image to resize with whatever the screen size is.
<script type="text/javascript">
var randnum = Math.random();
var inum = 3;
var rand1 = Math.round(randnum * (inum - 1)) + 1;
images = new Array();
images[1] = "bgImages/Blue_Venetian_Glass.jpg";
images[2] = "bgImages/Etched Glass.jpg";
images[3] = "bgImages/glass.jpg";
images[4] = "bgImages/Raindrops_on_Glass.jpg";
var image = images[rand1];
document.body.style.background = 'url("' + image + '") no-repeat center center fixed';
</script>
My question is "what do I need to make the image stretch"
Share Improve this question asked Sep 25, 2012 at 15:50 wesdfgfgdwesdfgfgd 6931 gold badge13 silver badges26 bronze badges3 Answers
Reset to default 2best way to achieve that effect would be to use a div with the image inside absolute positioned it that you can style.
HTML to add before </body>
:
<div align="center"><img src="images/BG.jpg" id="bg"></div>
CSS:
#bg {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
z-index: -5000;
}
Javascript:
var randnum = Math.random();
var inum = 3;
var rand1 = Math.round(randnum * (inum - 1)) + 1;
images = new Array();
images[1] = "bgImages/Blue_Venetian_Glass.jpg";
images[2] = "bgImages/Etched Glass.jpg";
images[3] = "bgImages/glass.jpg";
images[4] = "bgImages/Raindrops_on_Glass.jpg";
var image = images[rand1];
document.getElementById('bg').src = image;
You can use CSS3 background-size
Property.
Check this link.
http://www.w3schools./cssref/css3_pr_background-size.asp
Have you tried giving the image a css class and setting the height and width attributes of the class to 100%?
本文标签: javascriptStretching image to full screen on htmlStack Overflow
版权声明:本文标题:javascript - Stretching image to full screen on html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745421678a2657927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论