jQuery 如何设置背景尺寸(高度和宽度)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17353195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:52:28  来源:igfitidea点击:

How to set background size (height & width)?

javascriptjquerycsssizewidth

提问by Chris Lad

I'm very new to JavaScript.

我对 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%.

我想要做的是将背景图像设置为固定大小,因此可以说:width: 100%height: 100%.

回答by alex

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

您想使用background-size: 100%. 确保其浏览器支持与您支持的平台兼容。

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

回答by seanwbarker

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>
<div class-"img"></div>

回答by Tim Wasson

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

这只是简单的 CSS,而不是 jQuery。您可以background-size在此处阅读更多信息:http: //www.w3schools.com/cssref/css3_pr_background-size.asp