三大手机系统的字体: javascript
ios 系统php
android 系统css
winphone 系统html
各个手机系统有本身的默认字体,且都不支持微软雅黑 如无特殊需求,手机端无需定义中文字体,使用系统默认 英文字体和数字字体可以使用 Helvetica ,三种系统都支持前端
* 移动端定义字体的代码 */ body{font-family:Helvetica;}
对于只须要适配手机设备,使用px便可html5
对于须要适配各类移动设备,使用rem,例如只须要适配iPhone和iPad等分辨率差异比较挺大的设备java
rem配置参考:android
html {font-size:10px} @media screen and (min-width:480px) and (max-width:639px) { html { font-size: 15px } } @media screen and (min-width:640px) and (max-width:719px) { html { font-size: 20px } } @media screen and (min-width:720px) and (max-width:749px) { html { font-size: 22.5px } } @media screen and (min-width:750px) and (max-width:799px) { html { font-size: 23.5px } } @media screen and (min-width:800px) and (max-width:959px) { html { font-size: 25px } } @media screen and (min-width:960px) and (max-width:1079px) { html { font-size: 30px } } @media screen and (min-width:1080px) { html { font-size: 32px } }
三、移动端touch事件(区分webkit 和 winphone)
当用户手指放在移动设备在屏幕上滑动会触发的touch事件ios
如下支持webkitgit
如下支持winphone 8
移动设备上的web网页是有300ms延迟的,玩玩会形成按钮点击延迟甚至是点击失效。
如下是历史缘由:
2007年苹果发布首款iphone上IOS系统搭载的safari为了将适用于PC端上大屏幕的网页能比较好的展现在手机端上,使用了双击缩放(double tap to zoom)的方案,好比你在手机上用浏览器打开一个PC上的网页,你可能在看到页面内容虽然能够撑满整个屏幕,可是字体、图片都很小看不清,此时能够快速双击屏幕上的某一部分,你就能看清该部分放大后的内容,再次双击后能回到原始状态。
双击缩放是指用手指在屏幕上快速点击两次,iOS 自带的 Safari 浏览器会将网页缩放至原始比例。
缘由就出在浏览器须要如何判断快速点击上,当用户在屏幕上单击某一个元素时候,例如跳转连接<a href="#"></a>
,此处浏览器会先捕获该次单击,但浏览器不能决定用户是单纯要点击连接仍是要双击该部分区域进行缩放操做,因此,捕获第一次单击后,浏览器会先Hold一段时间t,若是在t时间区间里用户未进行下一次点击,则浏览器会作单击跳转连接的处理,若是t时间里用户进行了第二次单击操做,则浏览器会禁止跳转,转而进行对该部分区域页面的缩放操做。那么这个时间区间t有多少呢?在IOS safari下,大概为300毫秒。这就是延迟的由来。形成的后果用户纯粹单击页面,页面须要过一段时间才响应,给用户慢体验感受,对于web开发者来讲是,页面js捕获click事件的回调函数处理,须要300ms后才生效,也就间接致使影响其余业务逻辑的处理。
解决方案:
触摸事件的响应顺序
一、ontouchstart 二、ontouchmove 三、ontouchend 四、onclick
解决300ms延迟的问题,也能够经过绑定ontouchstart事件,加快对事件的响应
retina
:一种具有超高像素密度的液晶屏,一样大小的屏幕上显示的像素点由1个变为多个,如在一样带下的屏幕上,苹果设备的retina显示屏中,像素点1个变为4个
在高清显示屏中的位图被放大,图片会变得模糊,所以移动端的视觉稿一般会设计为传统PC的2倍
那么,前端的应对方案是:
设计稿切出来的图片长宽保证为偶数,并使用backgroud-size把图片缩小为原来的1/2
//例如图片宽高为:200px*200px,那么写法以下 .css{width:100px;height:100px;background-size:100px 100px;}
其它元素的取值为原来的1/2,例如视觉稿40px的字体,使用样式的写法为20px
.css{font-size:20px}
ios用户点击一个连接,会出现一个半透明灰色遮罩, 若是想要禁用,可设置-webkit-tap-highlight-color的alpha值为0,也就是属性值的最后一位设置为0就能够去除半透明灰色遮罩
a,button,input,textarea{-webkit-tap-highlight-color: rgba(0,0,0,0;)}
android用户点击一个连接,会出现一个边框或者半透明灰色遮罩, 不一样生产商定义出来额效果不同,可设置-webkit-tap-highlight-color的alpha值为0去除部分机器自带的效果
a,button,input,textarea{ -webkit-tap-highlight-color: rgba(0,0,0,0;) -webkit-user-modify:read-write-plaintext-only; }
-webkit-user-modify有个反作用,就是输入法再也不可以输入多个字符
另外,有些机型去除不了,如小米2
对于按钮类还有个办法,不使用a或者input标签,直接用div标签
<meta name="msapplication-tap-highlight" content="no">
.css{-webkit-appearance:none;}
input::-webkit-input-placeholder{color:#AAAAAA;} input:focus::-webkit-input-placeholder{color:#EEEEEE;}
ios能够,android不行~
在iOS中,默认状况下键盘是开启首字母大写的功能的,若是启用这个功能,能够这样:
<input type="text" autocapitalize="off" />
和英文输入默认自动首字母大写那样,IOS还作了一个功能,默认输入法会开启自动修正输入内容,这样的话,用户常常要操做两次。若是不但愿开启此功能,咱们能够经过input标签属性来关闭掉:
<input type="text" autocorrect="off" />
当移动设备横竖屏切换时,文本的大小会从新计算,进行相应的缩放,当咱们不须要这种状况时,能够选择禁止:
html { -webkit-text-size-adjust: 100%; }
须要注意的是,PC端的该属性已经被移除,该属性在移动端要生效,必须设置 ‘meta viewport’。
在iOS上,输入框默认有内部阴影,但没法使用 box-shadow 来清除,若是不须要阴影,能够这样关闭:
input, textarea { border: 0; /* 方法1 */ -webkit-appearance: none; /* 方法2 */ }
咱们先来看看回弹滚动在手机浏览器发展的历史:
在iOS上若是你想让一个元素拥有像 Native 的滚动效果,你能够这样作:
.xxx { overflow: auto; /* auto | scroll */ -webkit-overflow-scrolling: touch; }
PS:iScroll用过以后感受不是很好,有一些诡异的bug,这里推荐另一个 iDangero Swiper,这个插件集成了滑屏滚动的强大功能(支持3D),并且还有回弹滚动的内置滚动条,官方地址:
若是你不想用户能够选中页面中的内容,那么你能够在css中禁掉:
.user-select-none { -webkit-user-select: none; /* Chrome all / Safari all */ -moz-user-select: none; /* Firefox all (移动端不须要) */ -ms-user-select: none; /* IE 10+ */ }
在作移动端页面时,会发现全部a标签在触发点击时或者全部设置了伪类 :active 的元素,默认都会在激活状态时,显示高亮框,若是不想要这个高亮,那么你能够经过css如下方法来进行全局的禁止:
html { -webkit-tap-highlight-color: rgba(0, 0, 0, 0); }
但这个方法在三星的机子上无效,有一种妥协的方法是把页面非真实跳转连接的a标签换成其它标签,能够解决这个问题。
一般当你在手机或者pad上长按图像 img ,会弹出选项 存储图像 或者 拷贝图像,若是你不想让用户这么操做,那么你能够经过如下方法来禁止:
img { -webkit-touch-callout: none; }
移动端触摸按钮的效果,可明示用户有些事情正要发生,是一个比较好体验,可是移动设备中并无鼠标指针,使用css的hover并不能知足咱们的需求,还好国外有个激活css的active效果,代码以下,
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport"> <meta content="yes" name="apple-mobile-web-app-capable"> <meta content="black" name="apple-mobile-web-app-status-bar-style"> <meta content="telephone=no" name="format-detection"> <meta content="email=no" name="format-detection"> <style type="text/css"> a{-webkit-tap-highlight-color: rgba(0,0,0,0);} .btn-blue{display:block;height:42px;line-height:42px;text-align:center;border-radius:4px;font-size:18px;color:#FFFFFF;background-color: #4185F3;} .btn-blue:active{background-color: #357AE8;} </style> </head> <body> <div class="btn-blue">按钮</div> <script type="text/javascript"> document.addEventListener("touchstart", function(){}, true) </script> </body> </html>
兼容性ios5+、部分android 4+、winphone 8
要作到全兼容的办法,可经过绑定ontouchstart和ontouchend来控制按钮的类名
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<meta content="email=no" name="format-detection">
<style type="text/css">
a{-webkit-tap-highlight-color: rgba(0,0,0,0);} .btn-blue{display:block;height:42px;line-height:42px;text-align:center;border-radius:4px;font-size:18px;color:#FFFFFF;background-color: #4185F3;} .btn-blue-on{background-color: #357AE8;} </style> </head> <body> <div class="btn-blue">按钮</div> <script type="text/javascript"> var btnBlue = document.querySelector(".btn-blue"); btnBlue.ontouchstart = function(){ this.className = "btn-blue btn-blue-on" } btnBlue.ontouchend = function(){ this.className = "btn-blue" } </script> </body> </html>
21.屏幕旋转的事件和样式
事件
window.orientation,取值:正负90表示横屏模式、0和180表现为竖屏模式;
window.onorientationchange = function(){
switch(window.orientation){ case -90: case 90: alert("横屏:" + window.orientation); case 0: case 180: alert("竖屏:" + window.orientation); break; } }
样式
//竖屏时使用的样式 @media all and (orientation:portrait) { .css{} } //横屏时使用的样式 @media all and (orientation:landscape) { .css{} }
应对方案:触屏即播
$('html').one('touchstart',function(){ audio.play() })
HTML5 deviceMotion:封装了运动传感器数据的事件,能够获取手机运动状态下的运动加速度等数据。
<input type="file">
的accept 属性
<!-- 选择照片 --> <input type=file accept="image/*"> <!-- 选择视频 --> <input type=file accept="video/*">
此处添加了一个html5访问摄像头的功能
<!DOCTYPE HTML>
<html>
<body>
<video id="video" width="640" height="480" autoplay></video>
<button id="snap">Snap Photo</button>
<canvas id="canvas" width="640" height="480"></canvas>
<script>
// Put event listeners into place
window.addEventListener("DOMContentLoaded", function() { // Grab elements, create settings, etc. var canvas = document.getElementById("canvas"), context = canvas.getContext("2d"), video = document.getElementById("video"), videoObj = { "video": true }, errBack = function(error) { console.log("Video capture error: ", error.code); }; document.getElementById("snap").addEventListener("click", function() { context.drawImage(video, 0, 0, 640, 480); }); // Put video listeners into place if(navigator.getUserMedia) { // Standard navigator.getUserMedia(videoObj, function(stream) { video.src = stream; video.play(); }, errBack); } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed navigator.webkitGetUserMedia(videoObj, function(stream){ video.src = window.webkitURL.createObjectURL(stream); video.play(); }, errBack); } else if(navigator.mozGetUserMedia) { // Firefox-prefixed navigator.mozGetUserMedia(videoObj, function(stream){ video.src = window.URL.createObjectURL(stream); video.play(); }, errBack); } }, false); </script> </body> </html>
使用总结:
.css{ /*设置内嵌的元素在 3D 空间如何呈现:保留 3D*/ -webkit-transform-style: preserve-3d; /*(设置进行转换的元素的背面在面对用户时是否可见:隐藏)*/ -webkit-backface-visibility: hidden; }
开启硬件加速
保证动画流畅
.css { -webkit-transform: translate3d(0, 0, 0); -moz-transform: translate3d(0, 0, 0); -ms-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); }
设计高性能CSS3动画的几个要素
input::-webkit-input-speech-button {display: none}