meta基础知识H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 name='viewport' content='width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no' /> 忽略将页面中的数字识别为电话号码 name='format-detection' content='telephone=no' /> 忽略Android平台中对邮箱地址的识别 name='format-detection' content='email=no' /> 当网站添加到主屏幕快速启动方式,可隐藏地址栏,仅针对ios的safari name='apple-mobile-web-app-capable' content='yes' /> 将网站添加到主屏幕快速启动方式,仅针对ios的safari顶端状态条的样式 name='apple-mobile-web-app-status-bar-style' content='black' /> viewport模板 charset='utf-8'> content='width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no' name='viewport'> content='yes' name='apple-mobile-web-app-capable'> content='black' name='apple-mobile-web-app-status-bar-style'> content='telephone=no' name='format-detection'> content='email=no' name='format-detection'> rel='stylesheet' href='index.css'>这里开始内容 viewport模板 – target-densitydpi=device-dpi,android 2.3.5以下版本不支持 charset='utf-8'> name='viewport' content='width=750, user-scalable=no, target-densitydpi=device-dpi'> content='yes' name='apple-mobile-web-app-capable'> content='black' name='apple-mobile-web-app-status-bar-style'> content='telephone=no' name='format-detection'> content='email=no' name='format-detection'> rel='stylesheet' href='index.css'>这里开始内容 参考案例:http://action.weixin.qq.com/payact/readtemplate?t=mobile/2015/wxzfsht/index_tmpl 常见问题移动端如何定义字体font-family /* 移动端定义字体的代码 */body{font-family:Helvetica;} 参考《移动端使用字体的思考》 移动端字体单位font-size选择px还是rem 对于需要适配各种移动设备,使用rem,例如只需要适配iPhone和iPad等分辨率差别比较挺大的设备 rem配置参考: html{font-size:10px}@media screen and (min-width:321px) and (max-width:375px){html{font-size:11px}}@media screen and (min-width:376px) and (max-width:414px){html{font-size:12px}}@media screen and (min-width:415px) 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){html{font-size:25px}} 移动端touch事件(区分webkit 和 winphone) 以下支持webkit
TouchEvent
参数信息(changedTouches[0])
参考:https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent 以下支持winphone 8
移动端click屏幕产生200-300 ms的延迟响应 移动设备上的web网页是有300ms延迟的,玩玩会造成按钮点击延迟甚至是点击失效。 以下是历史原因,来源一个公司内一个同事的分享: 2007年苹果发布首款iphone上IOS系统搭载的safari为了将适用于PC端上大屏幕的网页能比较好的展示在手机端上,使用了双击缩放(double tap to zoom)的方案,比如你在手机上用浏览器打开一个PC上的网页,你可能在看到页面内容虽然可以撑满整个屏幕,但是字体、图片都很小看不清,此时可以快速双击屏幕上的某一部分,你就能看清该部分放大后的内容,再次双击后能回到原始状态。 双击缩放是指用手指在屏幕上快速点击两次,iOS 自带的 Safari 浏览器会将网页缩放至原始比例。 原因就出在浏览器需要如何判断快速点击上,当用户在屏幕上单击某一个元素时候,例如跳转链接,此处浏览器会先捕获该次单击,但浏览器不能决定用户是单纯要点击链接还是要双击该部分区域进行缩放操作,所以,捕获第一次单击后,浏览器会先Hold一段时间t,如果在t时间区间里用户未进行下一次点击,则浏览器会做单击跳转链接的处理,如果t时间里用户进行了第二次单击操作,则浏览器会禁止跳转,转而进行对该部分区域页面的缩放操作。那么这个时间区间t有多少呢?在IOS safari下,大概为300毫秒。这就是延迟的由来。造成的后果用户纯粹单击页面,页面需要过一段时间才响应,给用户慢体验感觉,对于web开发者来说是,页面js捕获click事件的回调函数处理,需要300ms后才生效,也就间接导致影响其他业务逻辑的处理。 解决方案:
触摸事件的响应顺序
解决300ms延迟的问题,也可以通过绑定ontouchstart事件,加快对事件的响应 什么是Retina 显示屏,带来了什么问题 在高清显示屏中的位图被放大,图片会变得模糊,因此移动端的视觉稿通常会设计为传统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系统中元素被触摸时产生的半透明灰色遮罩怎么去掉 ios用户点击一个链接,会出现一个半透明灰色遮罩, 如果想要禁用,可设置-webkit-tap-highlight-color的alpha值为0,也就是属性值的最后一位设置为0就可以去除半透明灰色遮罩 a,button,input,textarea{-webkit-tap-highlight-color: rgba(0,0,0,0;)} 部分android系统中元素被点击时产生的边框怎么去掉 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标签 winphone系统a、input标签被点击时产生的半透明灰色背景怎么去掉 name='msapplication-tap-highlight' content='no'> webkit表单元素的默认外观怎么重置 .css{-webkit-appearance:none;} webkit表单输入框placeholder的颜色值能改变么 input::-webkit-input-placeholder{color:#AAAAAA;}input:focus::-webkit-input-placeholder{color:#EEEEEE;} webkit表单输入框placeholder的文字能换行么 在textarea标签下都可以换行~ IE10(winphone8)表单元素默认外观如何重置 ::-ms-expand 适用于表单选择控件下拉箭头的修改,有多个属性值,设置它隐藏 (display:none) 并使用背景图片来修饰可得到我们想要的效果。 select::-ms-expand {display: none;} 禁用 radio 和 checkbox 默认样式 ::-ms-check 适用于表单复选框或单选按钮默认图标的修改,同样有多个属性值,设置它隐藏 (display:none) 并使用背景图片来修饰可得到我们想要的效果。 input[type=radio]::-ms-check,input[type=checkbox]::-ms-check{display: none;} 禁用PC端表单输入框默认清除按钮 当表单文本输入框输入内容后会显示文本清除按钮,::-ms-clear 适用于该清除按钮的修改,同样设置使它隐藏 (display:none) 并使用背景图片来修饰可得到我们想要的效果。 input[type=text]::-ms-clear,input[type=tel]::-ms-clear,input[type=number]::-ms-clear{display: none;} 禁止ios 长按时不触发系统的菜单,禁止ios&android长按时下载图片 .css{-webkit-touch-callout: none} 禁止ios和android用户选中文字 .css{-webkit-user-select:none} 参考《如何改变表单元素的外观(for Webkit and IE10)》 打电话发短信写邮件怎么实现 发短信,winphone系统无效 写邮件,可参考《移动web页面给用户发送邮件的方法》 模拟按钮hover效果 charset='utf-8'> content='width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no' name='viewport'> content='yes' name='apple-mobile-web-app-capable'> content='black' name='apple-mobile-web-app-status-bar-style'> content='telephone=no' name='format-detection'> content='email=no' name='format-detection'> 兼容性ios5+、部分android 4+、winphone 8 要做到全兼容的办法,可通过绑定ontouchstart和ontouchend来控制按钮的类名 charset='utf-8'> content='width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no' name='viewport'> content='yes' name='apple-mobile-web-app-capable'> content='black' name='apple-mobile-web-app-status-bar-style'> content='telephone=no' name='format-detection'> content='email=no' name='format-detection'> 屏幕旋转的事件和样式 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{}} audio元素和video元素在ios和andriod中无法自动播放 $('html').one('touchstart',function(){audio.play()}) 可参考《无法自动播放的audio元素》 摇一摇功能 手机拍照和上传图片 type=file accept='image/*'> type=file accept='video/*'> 使用总结: ios 有拍照、录像、选取本地图片功能 android侧是复写了layoutinflater 对textview做了统一处理 android使用以下代码,该接口只在微信浏览器下有效(感谢jationhuang同学提供) /*** 页面加入这段代码可使Android机器页面不再受到用户字体缩放强制改变大小* 但是会有一个1秒左右的延迟,期间可以考虑通过loading展示* 仅供参考*/(function(){if (typeof(WeixinJSBridge) == 'undefined') {document.addEventListener('WeixinJSBridgeReady', function (e) {setTimeout(function(){WeixinJSBridge.invoke('setFontSizeCallback',{'fontSize':0}, function(res) {alert(JSON.stringify(res));});},0);});} else {setTimeout(function(){WeixinJSBridge.invoke('setFontSizeCallback',{'fontSize':0}, function(res) {alert(JSON.stringify(res));});},0);}})(); ios使用-webkit-text-size-adjust禁止调整字体大小 body{-webkit-text-size-adjust: 100%!important;} 最好的解决方案: 整个页面用rem或者百分比布局 .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);} 取消input在ios下,输入的时候英文首字母的默认大写 autocapitalize='off' autocorrect='off' /> android 上去掉语音输入按钮 input::-webkit-input-speech-button {display: none} android 2.3 bug
android 4.x bug
设计高性能CSS3动画的几个要素
参考《High Performance Animations》 fixed bug
解决方案
参考 《移动端web页面使用position:fixed问题总结》 《使用iScroll.js解决ios4下不支持position:fixed的问题》 如何阻止windows Phone的默认触摸事件 目前解决方法是使用样式来禁用 html{-ms-touch-action: none;}/* 禁止winphone默认触摸事件 */ 参考 《Windows phone 8 touch support》 播放视频不全屏 常用的移动端框架zepto.js 语法与jquery几乎一样,会jquery基本会zepto~ 最新版本已经更新到1.16 官网:http:/// 中文(非官网):http://www./doc/zeptojs_api/ 常使用的扩展模块: 浏览器检测:https://github.com/madrobby/zepto/blob/master/src/detect.js tap事件:https://github.com/madrobby/zepto/blob/master/src/touch.js iscroll.js解决页面不支持弹性滚动,不支持fixed引起的问题~ 实现下拉刷新,滑屏,缩放等功能~ 最新版本已经更新到5.0 underscore.js笔者没用过,不过听说好用,推荐给大家~ 该库提供了一整套函数式编程的实用功能,但是没有扩展任何JavaScript内置对象。 最新版本已经更新到1.8.2 官网:http:/// 滑屏框架适合上下滑屏、左右滑屏等滑屏切换页面的效果 flex布局 /* ============================================================flex:定义布局为盒模型flex-v:盒模型垂直布局flex-1:子元素占据剩余的空间flex-align-center:子元素垂直居中flex-pack-center:子元素水平居中flex-pack-justify:子元素两端对齐兼容性:ios 4+、android 2.3+、winphone8+============================================================ */.flex{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;}.flex-v{-webkit-box-orient:vertical;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;}.flex-1{-webkit-box-flex:1;-webkit-flex:1;-ms-flex:1;flex:1;}.flex-align-center{-webkit-box-align:center;-webkit-align-items:center;-ms-flex-align:center;align-items:center;}.flex-pack-center{-webkit-box-pack:center;-webkit-justify-content:center;-ms-flex-pack:center;justify-content:center;}.flex-pack-justify{-webkit-box-pack:justify;-webkit-justify-content:space-between;-ms-flex-pack:justify;justify-content:space-between;} 示例:两端对齐 charset='utf-8'> content='width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no' name='viewport'> content='yes' name='apple-mobile-web-app-capable'> content='black' name='apple-mobile-web-app-status-bar-style'> content='telephone=no' name='format-detection'> content='email=no' name='format-detection'> 使用注意:flex下的子元素必须为块级元素,非块级元素在android2.3机器下flex失效 参考: FastClick消除在移动浏览器上触发click事件与一个物理Tap(敲击)之间的300延迟 参考《FastClick》 转载:http://peunzhang.cnblogs.com/ AD:【优秀主题】tob主题正式发布,够炫酷! 完美的自适应布局,完善的图片站配置 转载请注明出处大前端 ? 手机移动端WEB资源整合 |
|