javascript - Stretching image to full screen on html - Stack Overflow

I have this code which randomly shows a background image. I sized up the images and lower the bytes to

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 badges
Add a ment  | 

3 Answers 3

Reset to default 2

best 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%?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745421679a4626991.html

相关推荐

  • javascript - Stretching image to full screen on html - Stack Overflow

    I have this code which randomly shows a background image. I sized up the images and lower the bytes to

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信