最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set background size (height & width)? - Stack Overflow

programmeradmin3浏览0评论

I'm very new to JavaScript.

My code so far:

$("#Stage").css(
  "background-image",
  "url(BG.jpg)",
  "background-attachment", 
  "fixed"
);

What I want to do is have the background image at a set size so lets say: width: 100% and height: 100%.

I'm very new to JavaScript.

My code so far:

$("#Stage").css(
  "background-image",
  "url(BG.jpg)",
  "background-attachment", 
  "fixed"
);

What I want to do is have the background image at a set size so lets say: width: 100% and height: 100%.

Share Improve this question edited Nov 3, 2016 at 12:50 alex 490k204 gold badges889 silver badges991 bronze badges asked Jun 27, 2013 at 21:07 Chris LadChris Lad 3494 gold badges8 silver badges25 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 12

You want to use background-size: 100%. Ensure that its browser support is patible with your supported platforms.

$("#Stage").css({ 
     "background-image": "url(BG.jpg)", 
     "background-attachment": "fixed",
     "background-size": "100%"
});

This worked for me (inside a slider):

<!-- CSS -->
<style>
    div.img { 
        background-repeat: no-repeat;
        background-position: center center;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
    }
    div.img:nth-of-type(1) {background-image: url('img.jpg');}
    div.img:nth-of-type(2) {background-image: url('img.jpg'); 
    }
</style>

<!-- HTML -->
<div class="img"></div>

This is just simple CSS, not jQuery. You can read more about background-size here: http://www.w3schools./cssref/css3_pr_background-size.asp

发布评论

评论列表(0)

  1. 暂无评论