2014-03-30 61 views
0

我試圖使用對象適合,但它似乎不與新瀏覽器等非常兼容 - 我只是想要它覆蓋的空間不會被壓縮時調整大小,有沒有在CSS或jQuery的方式只是覆蓋空間而不是調整大小和壓扁?調整窗口大小的圖像大小?

回答

0

如果確定與另一元件而不是圖像標籤上使用的背景圖像,則可以使用background-size: cover這樣的:

#myDiv{ 
    background-image:url(/path/to/bg/image); 
    background-size: cover 
} 
+0

沒有,因爲我從數據庫中獲取的一切,我不能刪除的img標籤.. :( – belthazorNv

0

視口設置到後使用視width和height屬性設備寬度,這樣的:

<meta name="viewport" content="width=device-width, initial-scale=1.0" /> 

而且你的CSS:

width:100vw; height:100vh; /*this is like a percentage of the viewport width and height*/ 
+0

對我不起作用:還在綿延..我只是希望它涵蓋.. – belthazorNv

0

使用jQuery,

$(function() { 

     var theWindow = $(window), 
      $bg = $("#bg"), 
      aspectRatio = $bg.width()/$bg.height(); 

     function resizeBg() { 

      if ((theWindow.width()/theWindow.height()) < aspectRatio) { 
       $bg.removeClass() 
        .addClass('bgheight'); 
      } else { 
       $bg.removeClass() 
        .addClass('bgwidth'); 
      } 

     } 

     theWindow.resize(function() { 
      resizeBg(); 
     }).trigger("resize"); 

    }); 

demo here