search
HomeWeb Front-endHTML Tutorial如何解决PC端和移动端自适应问题? - 向婷风

   做网页时,我们通常需要考虑到不同电脑屏幕尺寸,以及不同手机屏幕大小等问题,解决样式发生改变的情况,那么如何解决呢?现在主要是采用自适应来解决高度,宽度的,以及图片自适应问题,下面就PC端和移动端来总结一下,通常进行自适应高度和宽度,图片时,一般与页面的布局存在关系。

1、最小尺寸分辨率1024*768(传统17寸显示器),则可以采用940px、960px、或者常用的980px作为最小宽度

2、1024*768之后稍大的分辨率就是1280*768了,则可以采用1200px或者1220px作为稍大的网页宽度

3、支持css3、html5的高级浏览器可以利用CSS3 Media Queries让网页在不同分辨率下自动调节布局标签

4、不支持css3、html5的脑残浏览器特别是

5、宽度自适应需要对每个显示模块进行不同宽度的计算,在做html布局时需要大量的计算与适配。

6、宽度自适应为不同宽度显示器写布局元素时常用的css

下面我们看下,如何用js和css来自适应屏幕的大小。

一:了解高度和宽度的基础

      下面用图片来说明:

     

    网页可见区域高宽为:document.body.clientHeight||document.body.clientWidth

    网页正文的区域高宽为:document.body.scrollHeight||document.body.scrollWidth(包括滚轮的长度)

    网页被卷去的上左区域:document.body.scrollTop||document.body.scrollLeft

二: css自适应高度

   1.两栏布局,左边固定,右边宽度自适应

<span style="color: #0000ff;">方法一:<br>//html部分<br><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="left"</span><span style="color: #0000ff;">></span>左边<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="bodyText"</span><span style="color: #0000ff;">></span>正文<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #000000;">
//css部分<br>*{margin:0;padding:0}<br>#left{float:left;width:200px;background:red;}<br>#bodyText{margin-left:200px;background:yellow;<br><br>方法二:<br>//html部分<br><div id="left">左边</div>
<br><div id="body">
<br>    <div id="bodyText">正文</div>
<br>
</div>
<br>//css部分<br>#left{float:left;width:200px;background:red;margin-right:-100%;}<br>#body{width:100%;float:left;}<br>#bodyText{margin-left:200px;background:yellow;}</span></span></span>

  2.三栏布局,两边定宽,中间自适应宽度

<span style="color: #0000ff;">方法一:<br><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="left"</span><span style="color: #0000ff;">></span>左边<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">>----注意和div的位置有关系</span>
<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="right"</span><span style="color: #0000ff;">></span>右边<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="center"</span><span style="color: #0000ff;">></span>中间<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">><br>//css部分<br>#left{width:200px;background:red;float:left;}<br>#center{width:auto;background:blue;}<br>#right{width:200px;background:yellow;float:right;}<br><br>方法二:<br>html部分:<br><div id="body">
<br>    <div id="center">中间</div>
<br>
</div>
<br><div id="left">左边</div>
<br><div id="right">右边</div>
<br>css部分:<br>#body{width:100%;float:left;} //设置浮动和width:100%<br>#body #center{background:red;margin-left:200px;margin-right:300px;} //margin-left:100%的使用方法<br>#left{width:200px;background:yellow;margin-left:-100%;float:left}<br>#right{width:300px;background:blue;margin-left:-300px;float:left}<br>-----如果设置为<span style="color: #0000ff;">margin-left:-100%</span>,则会跑到body的左边。<br>-----如果设置为<span style="color: #0000ff;">margin-left:-300px</span>(即right的宽度),则会跑到body的右边<br></span></span></span></span>

 3.关于最小宽度和最大宽度

   这里依然结合布局来看,如下面的代码:自适应宽度,从而改变布局。

<span style="color: #0000ff;">//html部分<br><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">='container'</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">='one'</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">='two'</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">='three'</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
 <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #000000;">

//css部分
#container{width:100%;}
.one{width:20%;background:red;}
.one,.two,.three{float:left; height:100px;}
.two{width:60%;background:yellow;}
.three{width:20%;background:blue;}
@media (max-width:800px){--如果浏览器小于800px
  .one{width:40%;}
  .two{width:60%}
  .three{width:100%}
}
@media (max-width:400px)--如果浏览器宽度小于400px
{
   .one{width:100%}
   .two{width:100%}
   .three{width:100%}
  
}</span></span></span></span></span>

  理解什么叫最小宽度和最大宽度,最小宽度指为元素设置的最小宽度,到达最小宽度后,缩放文本不会起到任何作用

最大宽度是所有元素所能达到的一个上限,不能再继续往上增加。

三: css处理自适应高度

<span style="color: #000000;">//html部分代码
</span><span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="fit"</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #000000;">
//css代码
html,body{margin:0;height:100%;}
#fit{width:200px;background:yellow;height:100%;border:1px solid red;}<br><br>--这里同时给html和body加样式,是为了兼容各大浏览器。<br>  <span style="font-size: small;"> IE 处于混杂模式时,body以窗口为高度参照,body设置为100%就可以使得页面和窗口一样高,body里面的嵌套div也可以扩展到窗口高度,</span><br><span style="font-size: small;">这样的话可以使布局适应浏览器窗口大小。</span><span style="font-size: small;"><span style="color: #ff0000;">窗体 》body》div  (html ,body {overflow:scroll}  一层滚动条) </span></span><br></span></span>
     但是当处于标准模式时,body以html标签为高度参照,html标签才以窗口为参照,所以仅仅body 100%,并不能使它的子div100% 占据整个屏幕
还要使得 html 100%使得 html获得窗口大小才行。窗体》html》body》div (html ,body {overflow:scroll}  两层滚动条 ,html的滚动条从来不会用到)

 父级随子级高度变化而自适应变化与子级随父级高度变化而变化

<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="fj"</span><span style="color: #0000ff;">></span><span style="color: #000000;">
   我是父级
   </span><span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="zj1"</span><span style="color: #0000ff;">></span>我是子级1<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
   <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="zj2"</span><span style="color: #0000ff;">></span>我是子级2<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">><br>//css部分<br>#fj{border:4px solid red;}<br>#zj1{border:2px solid yellow;}<br>#zj2{border:2px solid blue;}----这种情况下,父级高度随着子级div的高度自适应的改变</span></span></span></span>

如果子div使用了float属性,此时已经脱离标准流,父div不会随内容的高度变化而变化,解决的办法是在浮动的div下面,加一个空div,设置clear属性both

<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="fj"</span><span style="color: #0000ff;">></span><span style="color: #000000;">
   我是父级
   </span><span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="zj1"</span><span style="color: #0000ff;">></span>我是子级11111111111111111111111111<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
   <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="zj2"</span><span style="color: #0000ff;">></span><span style="color: #000000;">我是子级222222222222222222222222222222222222222222
  222222222222222222222222222</span><span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
   <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="clear"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="clear:both"</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">>------如果去掉这句话,则父级div高度,不会随着子级的高度变化而变化</span>
<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">><br>//css部分<br>#fj{border:4px solid black;}<br>#zj1{border:2px solid yellow;float:left}<br>#zj2{border:2px solid blue;float:left}<br></span></span></span></span></span>

高度的自适应的方法还有很多,这里不再列举。像height:auto等等。

四:js处理高度和宽度自适应问题

<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="div1"</span> <span style="color: #0000ff;">></span>222222222222222222222<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #000000;">
//js部分
function setHeight(obj)
{
  var temHeight=null;
  //FF
  if(window.innerHeight)
  {
    temHeight=window.innerHeight;//包括页面高度和滚动条高度
  }
  else 
  {
     temHeight=document.body&&document.body.clientHeight;
  }
  if(temHeight>document.body.clientHeight)//页面高度
  {
     oDiv.style.height=temHeight+"px";
  }
  else
  {
    oDiv.style.height=document.body.clientHeight+"px";
  }
}
window.onload=function()
{
  var oDiv=document.getElementById("div1");
  getHeight(oDiv);
}</span></span>

宽度自适应代码:

<span style="color: #000000;">function  setWidth(obj)<br>{
     var screenWidth = window.screen.width;    
     var width;
     var imgURL ;
     if (screenWidth >= 1440) <br>     {
         width = "1400px";
         imgURL = "1400.png";//设置不同分辨率下的图片
     }<br>     else if (1024 </span><span style="color: #0000ff;"><span style="color: #800000;"> screenWidth </span><span style="color: #ff0000;">&& screenWidth      {
         width </span><span style="color: #0000ff;">= "1200px"</span><span style="color: #ff0000;">;
         imgURL </span><span style="color: #0000ff;">= "1200.png"</span><span style="color: #ff0000;">;
     } <br>      else {
          width </span><span style="color: #0000ff;">= "980px"</span><span style="color: #ff0000;">;
          imgURL </span><span style="color: #0000ff;">= "980.png"</span><span style="color: #ff0000;">;
        }
       obj.style.width=<span style="color: #ff0000;">width ;</span><br>       obj.style.backgroundImage="<span style="color: #ff0000;">url(" + imgURL + ")</span>";
  })</span></span>

 五:移动端的自适应高度和宽度

     移动端的相对要简单些,首先,在网页代码的头部,加入一行viewport标签。

   

     viewport是网页默认的宽度和高度,上面的意思表示,网页的宽度默认等于设备屏幕的宽度,原始缩放比例为1,即网页初始大小占屏幕面积的100%。

    1:由于网页会根据屏幕宽度调整布局,所以不能使用绝对宽度的布局,也不能使用具有绝对宽度的元素。这一条非常重要。具体说,CSS代码不能指定像素宽度:width:xxx px;只能指定百分比宽度:<code>width: xx%;或者width:auto;

    2:一般使用em,尽量少使用px字体

    3:使用流动布局

    4:自适应网页设计”的核心,就是CSS3引入的media query模块。下载地址:http://download.csdn.net/download/song_121292057/8031781

    自动探测屏幕宽度,然后加载相应的CSS文件。

    <span style="color: #0000ff;"><span style="color: #800000;">link </span><span style="color: #ff0000;">rel</span><span style="color: #0000ff;">="stylesheet"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="text/css" <span style="color: #ff0000;"> media<span style="color: #0000ff;">="screen and (max-device-width: 400px)"<span style="color: #ff0000;"> href<span style="color: #0000ff;">="style.css" <span style="color: #0000ff;">/> <br><br></span></span></span></span></span></span><span style="color: #0000ff;">-------当屏幕小于400时,就加载style.css这个文件</span></span>

  5:除了用html标签加载CSS文件,还可以在现有CSS文件中加载。

  @import url("style2.css") screen and (max-device-width: 800px);//当小于800px屏幕时,就加载style2.css文件

  6:图片的自动缩放,比较简单。只要一行CSS代码:img{ max-width: 100%;}建议根据不同的屏幕分辨率,加载不同大小像素的图片。      

    移动端的自适应,大体上差不多就这么多,主要核心是利用mediaquery,根据不同的屏幕大小,实现不同的布局。代码可看上面的列子。这里不再重复写。


大概总结了一下自己遇到的问题,还有什么好的方法,请给我留言哈!

作者:向婷风

出处:http://www.cnblogs.com/jtjds/p/5480857.html

如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者

本人同意 转载文章之后必须在 文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。

 


 

 

 

    

    

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool