1:pc端响应式开发

火狐浏览器

打开菜单栏,点击开发者,选择我们的响应式设计 ctrl+shiift+M

谷歌浏览器

点击“show console”图标,打开模拟面板,使用"User Agent",选择所要模拟的机子,点击emulate按钮模拟,刷新浏览器。如要调整横竖屏,切换到"screen"

2:新建页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

</body>
</html>

meta标签

<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection"content="telephone=no, email=no" />

viewport

视图窗口,移动端特属的标签。一般使用下面这段代码即可:

<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />

上面的代码依次表示设置宽度为设备的宽度,默认不缩放,不允许用户缩放(即禁止缩放),在网页加载时隐藏地址栏与导航栏(ios7.1新增)。

width – // [pixel_value | device-width] viewport 的宽度,范围从 200 到 10,000,默认为 980 像素
height – // [pixel_value | device-height ] viewport 的高度,范围从 223 到 10,000
initial-scale – // float_value,初始的缩放比例 (范围从 > 0 到 10)
minimum-scale – // float_value,允许用户缩放到的最小比例
maximum-scale – // float_value,允许用户缩放到的最大比例
user-scalable – // [yes | no] 用户是否可以手动缩放
target-densitydpi = [dpi_value | device-dpi | high-dpi | medium-dpi | low-dpi] 目标屏幕像素密度

注:target-densitydpi屏幕像素密度和缩放有关,你可以试试修改这个​​demo​​,用手机看下实际效果。我一般不设置这个属性。

apple-mobile-web-app-capable

是否启动webapp功能,会删除默认的苹果工具栏和菜单栏。

<meta name="apple-mobile-web-app-capable" content="yes" />

apple-mobile-web-app-status-bar-style

当启动webapp功能时,显示手机信号、时间、电池的顶部导航栏的颜色。默认值为default(白色),可以定为black(黑色)和black-translucent(灰色半透明)。这个主要是根据实际的页面设计的主体色为搭配来进行设置。

<meta name="apple-mobile-web-app-status-bar-style" content="black" />

注:如果对​​apple-mobile-web-app-capable​​​和​​apple-mobile-web-app-status-bar-style​​不太理解,可查阅下面的参考资料第三篇文章,里面有截图说明。

telephone & email

忽略页面中的数字识别为电话号码

<meta name="format-detection" content="telephone=no" />

同样还有一个email识别

<meta name="format-detection" content="email=no" />

当然两者可以写在一起

<meta name="format-detection" content="telphone=no, email=no" />

其他meta

<!-- 启用360浏览器的极速模式(webkit) -->
<meta name="renderer" content="webkit">
<!-- 避免IE使用兼容模式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 -->
<meta name="HandheldFriendly" content="true">
<!-- 微软的老式浏览器 -->
<meta name="MobileOptimized" content="320">
<!-- uc强制竖屏 -->
<meta name="screen-orientation" content="portrait">
<!-- QQ强制竖屏 -->
<meta name="x5-orientation" content="portrait">
<!-- UC强制全屏 -->
<meta name="full-screen" content="yes">
<!-- QQ强制全屏 -->
<meta name="x5-fullscreen" content="true">
<!-- UC应用模式 -->
<meta name="browsermode" content="application">
<!-- QQ应用模式 -->
<meta name="x5-page-mode" content="app">
<!-- windows phone 点击无高光 -->
<meta name="msapplication-tap-highlight" content="no">

参考资料:

link标签

apple-touch-icon

如果​​apple-mobile-web-app-capable​​​设置为​​yes​​​了,那么在iPhone,iPad,iTouch的safari上可以使用添加到主屏按钮将网站添加到主屏幕上。而通过设置相应​​apple-touch-icon​​标签,则添加到主屏上的图标就会使用我们指定的图片。

以下是针对ox不同设备,选择一个最优icon。默认iphone的大小为60px,ipad为76px,retina屏乘以2倍。

<link rel="apple-touch-icon" href="touch-icon-iphone.png">
<link rel="apple-touch-icon" sizes="76x76" href="touch-icon-ipad.png">
<link rel="apple-touch-icon" sizes="120x120" href="touch-icon-iphone-retina.png">
<link rel="apple-touch-icon" sizes="152x152" href="touch-icon-ipad-retina.png">

ios7以前系统默认会对图标添加特效(圆角及高光),如果不希望系统添加特效,则可以用​​apple-touch-icon-precomposed.png​​​代替​​apple-touch-icon.png​

图标使用的优先级如下:

  • 如果没有跟相应设备推荐尺寸一致的图标,那个会优先使用比推荐尺寸大,但最接近推荐尺寸的图标。
  • 如果没有比推荐尺寸大的图标,会优先选择最接近推荐尺寸的图标。
  • 如些有多个图标符合推荐尺寸,会优先选择包含关键字precomposed的图标。

如果未在区域指定用link标签指定图标,会自动搜索网站根目录下以​​apple-touch-icon​​为前缀的png图标。

注:ios7不再为icon添加特效,ios7以前则默认为icon添加特效,除非icon有关键字​​-precomposed.png​​为后缀。

参考资料:

apple-touch-startup-image

同样基于​​apple-mobile-web-app-capable​​​设置为​​yes​​,可以用WebApp设置一个类似NativeApp的启动画面。

<link rel="apple-touch-startup-image" href="/startup.png">

和​​apple-touch-icon​​​不同,​​apple-mobile-web-app-capable​​不支持sizes属性,所以使用media来控制retina和横竖屏加载不同的启动画面。

// iPhone
<link href="apple-touch-startup-image-320x460.png" media="(device-width: 320px)" rel="apple-touch-startup-image" />

// iPhone Retina
<link href="apple-touch-startup-image-640x920.png" media="(device-width: 320px) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image" />

// iPhone 5
<link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)" href="apple-touch-startup-image-640x1096.png">

// iPad portrait
<link href="apple-touch-startup-image-768x1004.png" media="(device-width: 768px) and (orientation: portrait)" rel="apple-touch-startup-image" />

// iPad landscape
<link href="apple-touch-startup-image-748x1024.png" media="(device-width: 768px) and (orientation: landscape)" rel="apple-touch-startup-image" />

// iPad Retina portrait
<link href="apple-touch-startup-image-1536x2008.png" media="(device-width: 1536px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image" />

// iPad Retina landscape
<link href="apple-touch-startup-image-1496x2048.png"media="(device-width: 1536px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 2)"rel="apple-touch-startup-image" />

参考资料:

  • Launch Images
  • iOS Web App Icons & Startup Images
  • iPhone 5 Web App Startup Image

总结

空白页面模板,然后再根据具体情况在此基础上添加apple-touch-icon和apple-touch-startup-image

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection"content="telephone=no, email=no" />
<title>Document</title>
</head>
<body>

</body>
</html>

3:整体布局

移动端和pc端一样整体上布局也可以分为上中下三部分,如图:

移动端页面制作_ipad

一般来说,header和footer部分都为fixed定位,中间的内容区域可滚动。

fixed布局

常规的结构如下:

<header class="header fixed-top"></header>
<div class="wrap-page">
<section class="page"></section>
<section class="page"></section>
...
</div>
<footer class="footer fixed-bottom"></footer>

因为移动端单页面特性,所以每个page为一个页面,然后整体使用wrap-page包裹。考虑到可滚动的为page内容,所以我们得给wrap-page一个具体的高度,然后使用原生的​​-webkit-overflow-scrolling:touch;​​来实现滚动,当然对于不支持的,也可以使用iscroll来兼容,而iscroll同样也需要一个固定高度的容器来包裹可滚动的内容。

* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html, body, .wrap-page {
height: 100%;
}
.wrap-page {
-webkit-overflow-scrolling: touch;
}
.fixed-top {
position: fixed;
left: 0;
right: 0;
top: 0;
z-index: 960;
}

.fixed-bottom {
position: fixed;
left: 0;
right: 0;
bottom: 0;
z-index: 940;
}

虽然header和footer部分采用了fixed定位,脱离了文档流,但还是会挡住下面的内容,所以有必要对wrap-page设置上下的padding间隔,以防止header和footer遮挡page内容(假设header和footer高度各为44px):

.wrap-page {
padding-top: 44px;
padding-bottom: 44px;
}

看起来不错,不过如果碰到有些页面有header和footer,而有些页面只有header,而有些甚至有两个header部分,这么一刀切的方法显然不合适。按照这个结构只好通过js来给wrap-page添加不同的class来设置上下的padding。下面我们说下另一种用css就能解决的方法。

优化结构

这次我们把footer提到wrap-page上面,然后采用兄弟选择器,就可以解决刚才那个padding问题,​​ratchet​​就是采用该方法(经反馈,一些国产的安卓机对兄弟元素选择器支持不太好,所以这个方案适合高上大的玩,更新时间:2014-07-03)

<header class="header fixed-top"></header>
<footer class="footer fixed-bottom"></footer>
<div class="wrap-page">
<section class="page"></section>
<section class="page"></section>
...
</div>

这样我们就可以采用兄弟选择器,设置上下的padding:

.header ~ .wrap-page {
padding-top: 44px;
}
.footer ~ .wrap-page {
padding-bottom: 44px;
}

同样如果有二层header部分(如搜索框):

<header class="header fixed-top"></header>
<section class="header-sub"></section>
<footer class="footer fixed-bottom"></footer>
<div class="wrap-page">
<section class="page"></section>
<section class="page"></section>
...
</div>

还是采用兄弟选择器,将wrap-page的padding-top设置为header的高度加上header-sub的高度:

.header-sub ~ .wrap-page {
padding-top: 88px;
}

看起来不错,再也不用通过js来判断对wrap-page增删class了。

不过这个也同样有个问题,为了说明这个问题,我们还是回到移动端单页面特性这个上面,我们的页面是通过page的形式添加到wrap-page这个包裹上的,每次只显示一个而已。

<div class="wrap-page">
<section class="page"></section>
<section class="page" style="display:none;"></section>
<section class="page" style="display:none;"></section>
...
</div>

当然每个页面不可能只有content部分,也会有header和footer,不同页面存在不同的header或footer这是绝对可能的。那么如果header和footer部分也通过这种显示隐藏的方式来搞呢?

如:我们从一个有header和footer的页面,切换到一个只有header的页面,且header改变,就会成为下面这样:

<header class="header fixed-top" style="display:none;"></header>
<header class="header fixed-top"></header>
<footer class="footer fixed-bottom" style="display:none;"></footer>

这样虽然footer隐藏了,但是对于下面这条样式同样还是会解析生效,wrap-page会有44px的padding-bottom

.footer ~ .wrap-page {
padding-bottom: 44px;
}

所以如果我们采用这种布局,header和footer绝对不能采用显示显示隐藏的方式来搞,而应该采用替换形式,没有则删除。具体可以参考ratchet的实现方式

绝对定位布局

关键在于设置wrap-page的top,bottom的距离为header和footer的高度。

css代码如下:

.header,.footer,.wrap-page{
position:absolute;
left:0;
right:0;
}
.header,.footer{
height:44px;
background-color: #fff;
text-align: center;
z-index:900;
line-height:44px;
}
.header{
top: 0;
border-bottom: 1px solid #f00;
}
.footer{
bottom: 0;
border-top: 1px solid #f00;
}
.wrap-page{
top: 44px;
bottom: 44px;
overflow-y:auto;
-webkit-overflow-scrolling:touch;
}
.page{
padding: 10px;
}
.page p{
margin-bottom: 10px;
}

这个布局的缺陷在于滚动的时候地址栏不隐藏,safari浏览器可以通过下面js代码来隐藏地址栏,其他浏览器经测试不可以

window.addEventListener('load', function(){
setTimeout(function(){ window.scrollTo(0, 1); }, 100);
});

如果你实在要除掉浏览器的地址栏和工具栏,可以设置meta标签为应用模式,参考新建空白页面的其他meta部分

<!-- UC应用模式 -->
<meta name="browsermode" content="application">
<!-- QQ应用模式 -->
<meta name="x5-page-mode" content="app">

flex布局

可以通过这个简单的demo来测试:flex layout demo

设置body为flex布局,方向为垂直方向,wrap-page的flex为1。这个跟上面的绝对定位一样,还是滚动的时候地址栏不隐藏,safari同样可以通过js来搞定,其他浏览器不可以

body {
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-orient: vertical;
-ms-flex-direction: column;
-webkit-flex-direction: column;
flex-direction: column;
}

.wrap-page {
-webkit-box-flex: 1;
-ms-flex: 1;
-webkit-flex: 1;
flex: 1;
}

.header,.footer{
height:44px;
background-color: #fff;
text-align: center;
line-height:44px;
position:relative;
z-index:990;
}
.header{
border-bottom: 1px solid #f00;
}
.footer{
border-top: 1px solid #f00;
}
.wrap-page{
overflow-y:auto;
-webkit-overflow-scrolling:touch;
}
.page{