移动端页面制做

1:pc端响应式开发

火狐浏览器

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

谷歌浏览器

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

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

视图窗口,移动端特属的标签。通常使用下面这段代码便可:css

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

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

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功能,会删除默认的苹果工具栏和菜单栏。java

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

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

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

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

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

telephone & email

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

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

一样还有一个email识别windows

<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" /> 

参考资料:

总结

空白页面模板,而后再根据具体状况在此基础上添加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端同样总体上布局也能够分为上中下三部分,如图:

layout template

通常来讲,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{ padding: 10px; } .page p{ margin-bottom: 10px; }

总结

由于fixed定位,滚动的时候bug太多,特别是有表单元素的时候得慎用;而flex布局兼容方面有必定问题,好像性能也不是很好,何况若是是在body下面直接布局的话,只有上中下这几个元素还好,若是再添加上弹窗,panel什么的子元素搞很差还有问题得深刻;因此选择绝对定位相对来讲仍是比较靠谱的。而优化的元素位置关系,由于国产的安卓手机太多,有些还不太支持,再加上隐藏的元素选择器还有效,因此暂时不考虑。

最后咱们通常采用常规结构的绝对定位来布局。

具体页面代码以下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />
<meta content="yes"name="apple-mobile-web-app-capable"/>
<meta content="black"name="apple-mobile-web-app-status-bar-style"/>
<meta name="format-detection"content="telphone=no"/>
<title>绝对定位布局</title>
<style type="text/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;}

</style>
</head>
<body>
<header class="header">header</header>

<div class="wrap-page">
<div class="page">
<p>伴随着今天全国铁路火车票预售期的大调整,2015年火车票抢购的大幕即将拉开。本周日将开始发售春运首日的火车票。目前,旅客能够选择五种方式购买火车票,分别是经过网络购票、电话订票、火车站窗口购票、手机APP客户端购票和自助售票机购票等几种方式。本报今日推出年底火车票购票攻略,但愿为您找到适合本身的购票方式。

网络购票

提早添加联系人可缩短购票时间

的方式是近两年最常被旅客使用的一种购票方法,选择网络购票的旅客须要先准备一台网速较快的电脑,并开通网银或支付宝。

购票时,登陆www.12306.cn网站,首次用此台电脑购票须要先下载压缩包,安装网站证书;首次购票旅客须要提早填写姓名电话注册,并激活网站的帐号。为了使购票更为顺畅,建议旅客先添加经常使用联系人,购票时可快速覆盖信息,缩短期。

随后,旅客只需输入车次或输入起始站、终到站、发车日期,如有票,可点击预订。确认订单后,进入支付页面,经过网银完成支付,并于发车前到售票口或代售点取票。

特别提示:60岁以上老人可优先安排下铺

日前,12306的网络售票系统进行了优化调整,调整后系统可以自动识别年龄在60岁以上的旅客并优先安排下铺。此外,针对多人同行一份订单的状况,系统也会自动分配相邻座位。须要提醒旅客的是,此项优先必须在车票有富余的车次,才能实现优化功能。还须要提醒旅客的是,订单确认后,要在45分钟内完成支付,过期将取消订单。单日取消三次订单的用户,系统将暂时冻结该用户订票的服务,第二天恢复。一个身份证号只能买一张票,一次最多能买5张车票。

预售期逐步延长至60天,是最先发售车票的一种方式,旅客无需到现场排队,操做方便。经过电脑,车次、到发时间、余票等各项信息一目了然;乘车前取票便可,无需另找时间取票。

须下载证书、注册;须要45分钟内用网银或银联支付;不适宜在非私人电脑安装网上支付票款证书。</p>
</div>
</div>
<footer class="footer">footer</footer>
</body>
</html>

4:重置样式 cssreset

根据目前咱们你们的使用习惯进行了一些清零及移动端的特色添加一些基础样式

html { font-family: "Helvetica Neue", Helvetica, STHeiTi, Arial, sans-serif; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; font-size: 62.5%; } body { margin: 0; font-size: 1.4rem; line-height: 1.5; color: #333333; background-color: white; height: 100%; overflow-x: hidden; -webkit-overflow-scrolling: touch; } article, aside, details, figcaption, figure, footer, header, hgroup, main, nav, section, summary { display: block; } audio, canvas, progress, video { display: inline-block; vertical-align: baseline; } audio:not([controls]) { display: none; height: 0; } [hidden], template { display: none; } svg:not(:root) { overflow: hidden; } a { background: transparent; text-decoration: none; -webkit-tap-highlight-color: transparent; color: #0088cc; } a:active { outline: 0; } a:active { color: #006699; } abbr[title] { border-bottom: 1px dotted; } b, strong { font-weight: bold; } dfn { font-style: italic; } mark { background: #ff0; color: #000; } small { font-size: 80%; } sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; } sup { top: -0.5em; } sub { bottom: -0.25em; } img { border: 0; vertical-align: middle; } hr { -moz-box-sizing: content-box; box-sizing: content-box;height:0;}pre { overflow: auto;white-space: pre;white-space: pre-wrap;word-wrap: break-word;}code, kbd, pre, samp { font-family: monospace, monospace;font-size:1em;}button, input, optgroup, select, textarea { color: inherit;font: inherit;margin:0;}button { overflow: visible;}button, select { text-transform: none;}button, html input[type="button"], input[type="reset"], input[type="submit"] { -webkit-appearance: button;cursor: pointer;}button[disabled], html input[disabled] { cursor: default;}button::-moz-focus-inner, input::-moz-focus-inner { border:0;padding:0;}input { line-height: normal;}input[type="checkbox"], input[type="radio"] { box-sizing: border-box;padding:0;}input[type="number"]::-webkit-inner-spin-button, input[type="number"]::-webkit-outer-spin-button { height: auto;}input[type="search"] { -webkit-appearance: textfield;-moz-box-sizing: border-box;-webkit-box-sizing: border-box;box-sizing: border-box;}input[type="search"]::-webkit-search-cancel-button, input[type="search"]::-webkit-search-decoration { -webkit-appearance: none;}fieldset { border:1px solid #c0c0c0;margin:0 2px;padding:0.35em 0.625em 0.75em;}legend { border:0;padding:0;}textarea { overflow: auto;resize: vertical;}optgroup { font-weight: bold;}table { border-collapse: collapse;border-spacing:0;}td, th { padding:0;}html, button, input, select, textarea { font-family:"Helvetica Neue", Helvetica, STHeiTi, Arial, sans-serif;}h1, h2, h3, h4, h5, h6, p, figure, form, blockquote { margin:0;}ul, ol, li, dl, dd { margin:0;padding:0;}ul, ol { list-style: none outside none;}h1, h2, h3 { line-height:2;font-weight: normal;}h1 { font-size:1.8rem;}h2 { font-size:1.6rem;}h3 { font-size:1.4rem;}input::-moz-placeholder, textarea::-moz-placeholder { color:#cccccc;}input:-ms-input-placeholder, textarea:-ms-input-placeholder { color:#cccccc;}input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color:#cccccc;} * { -webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;}

有两点须要说明:

一、关于moz前缀

由于本人习惯使用firefox浏览器,因此没有舍弃moz前缀,并且moz前缀也很少,就几个而已,因此没有去掉。若有代码洁癖者,可去掉。

二、关于字体

font-family: "Helvetica Neue", Helvetica, STHeiTi, Arial, sans-serif; 

IOS

iOS 4.0+ 使用英文字体 Helvetica Neue,以前的iOS版本降级使用 Helvetica,中文字体设置为华文黑体STHeiTi。

Android

原生Android下中文字体与英文字体都选择默认的无衬线字体,4.0以前版本英文字体原生Android使用的是Droid Sans,中文字体原生Android会命中Droid Sans Fallback,4.0+ 中英文字体都会使用原生Android新的Roboto字体。其余第三方Android系统也一致选择默认的无衬线字体。

最后加上Arial字体方便pc端查看效果。

相关文章
相关标签/搜索