点击“show console”图标,打开模拟面板,使用"User Agent",选择所要模拟的机子,点击emulate按钮模拟,刷新浏览器。如要调整横竖屏,切换到"screen"javascript
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> </body> </html>
<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" />
视图窗口,移动端特属的标签。通常使用下面这段代码便可: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,用手机看下实际效果。我通常不设置这个属性。前端
是否启动webapp功能,会删除默认的苹果工具栏和菜单栏。java
<meta name="apple-mobile-web-app-capable" content="yes" />
当启动webapp功能时,显示手机信号、时间、电池的顶部导航栏的颜色。默认值为default(白色),能够定为black(黑色)和black-translucent(灰色半透明)。这个主要是根据实际的页面设计的主体色为搭配来进行设置。ios
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
注:若是对apple-mobile-web-app-capable
和apple-mobile-web-app-status-bar-style
不太理解,可查阅下面的参考资料第三篇文章,里面有截图说明。web
忽略页面中的数字识别为电话号码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" />
<!-- 启用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">
参考资料:
若是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
图标使用的优先级以下:
若是未在区域指定用link标签指定图标,会自动搜索网站根目录下以apple-touch-icon
为前缀的png图标。
注:ios7再也不为icon添加特效,ios7之前则默认为icon添加特效,除非icon有关键字-precomposed.png
为后缀。
参考资料:
一样基于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>
移动端和pc端同样总体上布局也能够分为上中下三部分,如图:
通常来讲,header和footer部分都为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">
能够经过这个简单的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: