網頁寬高自適應大小

來源:互聯網
上載者:User

標籤:高度自適應   style   class   blog   code   color   

如今,顯示器的解析度越來越多,終端也變得多樣化,web開發頁面的自適應問題越來越多,如果不做處理,一旦顯示器的解析度發生變化,展示的內容可能出現許多意料之外的排版問題。關於不同終端的展示問題可以通過響應式布局來實現,而不需要響應式布局時我們需要自己來避免上述問題。

寬度自適應:

1、設定最外層容器(如 DIV)的 width 為 100%;

2、如果網站頭部有圖片展示,那就不能簡單設定寬度為 100%,會出現 repeat 的情況,或者圖片大小超出最外層容器寬度,此時可以設定最外層容器寬度為固定值:960px、980px、1000px等,並設定 margin:0 auto ;

3、如果覺得在較大解析度的顯示器上顯示 960px 寬度顯得不夠大方美觀,可以根據不同的解析度設定不同的寬度(同樣要設定 margin:0 auto),並製作相應大小的圖片替換即可:

$(function(){    var screenWidth = window.screen.width;        var width;    var imgURL ;    if (screenWidth >= 1440) {        width = "1400px";        imgURL = "1400.png";    } else if (1024 < screenWidth && screenWidth < 1440) {        width = "1200px";        imgURL = "1200.png";    } else {        width = "980px";        imgURL = "980.png";    }    $obj.css("width", width);  //$obj為要設定寬度的jQuery對象    $img.css("backgroundImage","url(" + imgURL + ")");  //$img為要設定背景的jQuery對象})    

高度自適應:

1、可直接設定最外層容器以及 html、body 的 height 為 100%;

2、有時,網頁的填充內容會根據不同的許可權或者資料的完整程度顯示出不一樣的大小,這樣,我們就需要比較頁面的大小和顯示器的解析度高度再做相應的調整:

function autoHeight(objId){    var nowHeight;    if (window.innerHeight){//FF        nowHeight = window.innerHeight;    }else{        nowHeight = document.documentElement.clientHeight;    }    if(nowHeight > document.body.clientHeight){        document.getElementById(objId).style.height = nowHeight  + ‘px‘;    }else{        document.getElementById(objId).style.height = document.body.clientHeight + ‘px‘;    }}

3、如果頁面有頁尾(著作權資訊等),採用情況2的方法可能會使頁尾懸於頁面中間,這時,頁面往往會是 header、main、footer 這樣的結構,在外面會有一個外層容器 container,方法2就是設定該外層容器的高度。當然,我們可以在擷取到 container 的新的高度之後減去 header 和 footer 的高度就可以設定 main 的高度了,這樣可以避免 footer 出現在頁面中間的情況了。

此外,我們還有另一種方式解決 footer 的問題:position。

設定 container 的 position:relative , main 和 footer 的 position:absolute(其餘css屬性略去):

#container{  position:relative;  }#main{  position:absolute;  top:80px;    /*header 的高度*/  bottom:40px;      /*footer 的高度*/} #footer{  position:absolute;  bottom:0;} 

 這樣結合上面寬度進行設定時,發現設定了 position 之後,margin:0 auto就失效了,因為脫離了文檔流的緣故,所以我們需要設定 container 的 margin-left 為寬度的一半的負值即可,即:

$(function(){    var screenWidth = window.screen.width;        var width;    var imgURL ;    var left;    if (screenWidth >= 1440) {        width = "1400px";        left = "-700px";        imgURL = "1400.png";    } else if (1024 < screenWidth && screenWidth < 1440) {        width = "1200px";        left = "-600px";        imgURL = "1200.png";    } else {        width = "980px";        left = "-490px";        imgURL = "980.png";    }    $obj.css({"width": width,"margin-left":left });  //$obj為要設定寬度的jQuery對象    $img.css("backgroundImage","url(" + imgURL + ")");  //$img為要設定背景的jQuery對象})    

以上僅是本人使用過的方法的思路,沒有具體實現的 demo,可根據實際情況考慮使用怎樣的方式,還會有其他更好的解決方案是我沒有想到的,學習中···

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.