2011-12-05 41 views
4

我想要使用jquery獲取瀏覽器的高度和寬度,並比使用該信息調整我的圖像的大小來適應該尺寸。無論是在筆記本電腦還是大型監視器上查看,我都希望圖像在每個頁面上全屏顯示。我現在擁有1280 dpi標準寬度的所有圖像。確定瀏覽器的高度和寬度來調整圖像到全屏

要查看我到目前爲止我已經發布我的代碼我的NYU帳戶:http://i5.nyu.edu/~ejs426/

+0

嗯,這很方便...你認爲可以發佈您的代碼* *在這裏,其中*我們是?順便說一句,你不關心你的圖像的長寬比嗎? –

回答

1

這完全可以利用CSS來實現。我建議您查看本教程:

http://css-tricks.com/3458-perfect-full-page-background-image/

下面的代碼represnts做事的超級簡單的CSS3的方式。該教程也提供了其他選擇。

html { 
     background: url(images/bg.jpg) no-repeat center center fixed; 
     -webkit-background-size: cover; 
     -moz-background-size: cover; 
     -o-background-size: cover; 
     background-size: cover; 
} 
0

這是我在過去使用,設置默認的高度和寬度,然後試圖通過嘗試檢索各種視口的尺寸來計算的話。

var myWidth = 800; 
var myHeight = 600; 

    if ($.browser.msie) { 
     if (typeof (window.innerWidth) == 'number') { 

      myWidth = window.innerWidth; 
      myHeight = window.innerHeight; 

     } 
     else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { 

      //IE 6+ in 'standards compliant mode' 

      myWidth = document.documentElement.clientWidth; 
      myHeight = document.documentElement.clientHeight; 

     } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) { 

      //IE 4 compatible 

      myWidth = document.body.clientWidth; 
      myHeight = document.body.clientHeight; 

     } 
    } 
    else { 
     myWidth = $(window).width(); 
     myHeight = $(window).height(); 
    } 
6

jQuery的

var W = $(window).width(), 
    H = $(window).height(); 

$('img').height(H).width(W); 

在調整

function imgsize() { 
    var W = $(window).width(), 
     H = $(window).height(); 

    $('img').height(H).width(W); 
} 
$(window).bind('resize', function() { imgsize(); }); 

CSS

img {height: 100%; width: 100%;} 
+0

非常感謝!我遇到的唯一問題是在img中調整大小,因爲我的圖像在數組中。我也希望他們保持寬高比。建議? 這是我有我的圖片: $(函數(){VAR 圖像= [ '編輯/ image1.jpg', '編輯/ image2.jpg', '編輯/ image3.jpg',「社論/image4.jpg','editorial/image5.jpg','editorial/image6.jpg','editorial/image7.jpg','editorial/image8.jpg']; – ElizabethS

相關問題