码迷,mamicode.com
首页 > Web开发 > 详细

网页宽高自适应大小

时间:2014-06-25 21:28:36      阅读:378      评论:0      收藏:0      [点我收藏+]

标签:高度自适应   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,可根据实际情况考虑使用怎样的方式,还会有其他更好的解决方案是我没有想到的,学习中···

 

网页宽高自适应大小,布布扣,bubuko.com

网页宽高自适应大小

标签:高度自适应   style   class   blog   code   color   

原文地址:http://www.cnblogs.com/ntt1219/p/3748119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!