2013-06-27 109 views
2

我對JavaScript很陌生。如何設置背景大小(高度和寬度)?

我迄今爲止代碼:

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

我想要做的是有在一組大小的背景圖像,讓我們說:width: 100%height: 100%

回答

12

您想要使用background-size: 100%。確保其瀏覽器支持與您支持的平臺兼容。

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

謝謝,這工作,但由於某種原因,「背景大小」:「100%100%」不工作可能的東西使用我正在使用的程序 - Edge Animate ...任何關於如何獲得100%高度和寬度以及將圖像拉伸到高度和寬度的想法..? –

+0

我可能會有點晚,但對於看到此評論的人:background-size:100%100%;將圖像拉伸至容器的100%高度和寬度,而不是圖像。因此,如果您希望圖片保持初始尺寸,您需要爲寬度或高度設置「自動」,例如背景大小:100%自動; (將圖像拉伸至容器的100%寬度並通過初始圖像尺寸的比率自動設置高度)。 – Kathara

0

這爲我工作(滑塊內):

<!-- 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> 
相關問題