本资料转载自他人。。。做者名字忘了,固未署上javascript
A useful tools or tips list for mobile web application developingphp
这个项目收集移动端开发所须要的一些资源与小技巧css
可基于下方所列出的统计数据来决定您要兼容的设备及浏览器。html
百度移动统计前端
友盟指数html5
CNZZ 桌面及移动端浏览器统计java
全球移动端浏览器统计node
HTML5 与 CSS3 技术应用评估android
各类奇妙的hackios
Firefox 浏览器内置了 自定义设计视图
的功能,能够经过 Firefox->Web 开发者->自定义设计视图
(或者摁下 Shift + Ctrl + m
)。相比网络工具,运行更加流畅,无需联网。
判断 iPad 和 iPhone 的版本和状态的 CSS 媒体查询代码
https://quirktools.com/screenfly/
媒体查询经常使用样式表:
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css"> // 竖放加载 <link rel="stylesheet" media="all and (orientation:landscape)"href="landscape.css"> // 横放加载 //竖屏时使用的样式 <style media="all and (orientation:portrait)" type="text/css"> #landscape { display: none; } </style> //横屏时使用的样式 <style media="all and (orientation:landscape)" type="text/css"> #portrait { display: none; } </style>
It’s not a web app. It’s an app you install from the web.
移动Web开发,4行代码检测浏览器是否支持position:fixed
移动Web开发实践——解决position:fixed自适应BUG
指尖下的js ——多触式web前端开发之一:对于Touch的处理
meta标签大全 http://segmentfault.com/blog/ciaocc/1190000002407912
meta标签,这些meta标签在开发webapp时起到很是重要的做用
<meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0" 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标签表示:强制让文档的宽度与设备的宽度保持1:1,而且文档最大的宽度比例是1.0,且不容许用户点击屏幕放大浏览;
尤为要注意的是content里多个属性的设置必定要用分号+空格来隔开,若是不规范将不会起做用。
注意根据 public_00 提供的资料补充,content 使用分号做为分隔,在老的浏览器是支持的,但不是规范写法。
规范的写法应该是使用逗号分隔,参考 Safari HTML Reference - Supported Meta Tags 和 Android - Supporting Different Screens in Web Apps
其中:
第二个meta标签是iphone设备中的safari私有meta标签,它表示:容许全屏模式浏览;
第三个meta标签也是iphone的私有标签,它指定的iphone中safari顶端的状态条的样式;
第四个meta标签表示:告诉设备忽略将页面中的数字识别为电话号码
在设置了initial-scale=1 以后,咱们终于能够以1:1 的比例进行页面设计了。
关于viewport,还有一个很重要的概念是:iphone 的safari 浏览器彻底没有滚动条,并且不是简单的“隐藏滚动条”,
是根本没有这个功能。iphone 的safari 浏览器实际上从一开始就完整显示了这个网页,而后用viewport 查看其中的一部分。
当你用手指拖动时,其实拖的不是页面,而是viewport。浏览器行为的改变不止是滚动条,交互事件也跟普通桌面不同。
(请参考:指尖的下JS 系列文章)
更详细的 viewport 相关的知识也能够参考
function orientationChange() { switch(window.orientation) { case 0: alert("肖像模式 0,screen-width: " + screen.width + "; screen-height:" + screen.height); break; case -90: alert("左旋 -90,screen-width: " + screen.width + "; screen-height:" + screen.height); break; case 90: alert("右旋 90,screen-width: " + screen.width + "; screen-height:" + screen.height); break; case 180: alert("风景模式 180,screen-width: " + screen.width + "; screen-height:" + screen.height); break; };};
addEventListener('load', function(){ orientationChange(); window.onorientationchange = orientationChange; });
click 事件由于要等待单击确认,会有 300ms 的延迟,体验并非很好。
开发者大多数会使用封装的 tap 事件来代替click 事件,所谓的 tap 事件由 touchstart 事件 + touchmove 判断 + touchend 事件封装组成。
Creating Fast Buttons for Mobile Web Applications
Eliminate 300ms delay on click events in mobile Safari
携程 UED 整理的 Webkit CSS 文档 ,全面、方便查询,下面为经常使用属性。
①“盒模型”的具体描述性质的包围盒块内容,包括边界,填充等等。
-webkit-border-bottom-left-radius: radius; -webkit-border-top-left-radius: horizontal_radius vertical_radius; -webkit-border-radius: radius; //容器圆角 -webkit-box-sizing: sizing_model; 边框常量值:border-box/content-box -webkit-box-shadow: hoff voff blur color; //容器阴影(参数分别为:水平X 方向偏移量;垂直Y 方向偏移量;高斯模糊半径值;阴影颜色值) -webkit-margin-bottom-collapse: collapse_behavior; 常量值:collapse/discard/separate -webkit-margin-start: width; -webkit-padding-start: width; -webkit-border-image: url(borderimg.gif) 25 25 25 25 round/stretch round/stretch; -webkit-appearance: push-button; //内置的CSS 表现,暂时只支持push-button
②“视觉格式化模型”描述性质,肯定了位置和大小的块元素。
direction: rtl unicode-bidi: bidi-override; 常量:bidi-override/embed/normal
③“视觉效果”描述属性,调整的视觉效果块内容,包括溢出行为,调整行为,能见度,动画,变换,和过渡。
clip: rect(10px, 5px, 10px, 5px) resize: auto; 常量:auto/both/horizontal/none/vertical visibility: visible; 常量: collapse/hidden/visible -webkit-transition: opacity 1s linear; 动画效果 ease/linear/ease-in/ease-out/ease-in-out -webkit-backface-visibility: visibler; 常量:visible(默认值)/hidden -webkit-box-reflect: right 1px; 镜向反转 -webkit-box-reflect: below 4px -webkit-gradient(linear, left top, left bottom, from(transparent), color-stop(0.5, transparent), to(white)); -webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)));; //CSS 遮罩/蒙板效果 -webkit-mask-attachment: fixed; 常量:fixed/scroll -webkit-perspective: value; 常量:none(默认) -webkit-perspective-origin: left top; -webkit-transform: rotate(5deg); -webkit-transform-style: preserve-3d; 常量:flat/preserve-3d; (2D 与3D)
④“生成的内容,自动编号,并列出”描述属性,容许您更改内容的一个组成部分,建立自动编号的章节和标题,和操纵的风格清单的内容。
content: “Item” counter(section) ” “; This resets the counter. First section >two section three section counter-increment: section 1; counter-reset: section;
⑤“分页媒体”描述性能与外观的属性,控制印刷版本的网页,如分页符的行为。
page-break-after: auto; 常量:always/auto/avoid/left/right page-break-before: auto; 常量:always/auto/avoid/left/right page-break-inside: auto; 常量:auto/avoid
⑥“颜色和背景”描述属性控制背景下的块级元素和颜色的文本内容的组成部分。
-webkit-background-clip: content; 常量:border/content/padding/text -webkit-background-origin: padding; 常量:border/content/padding/text -webkit-background-size: 55px; 常量:length/length_x/length_y
⑦ “字型”的具体描述性质的文字字体的选择范围内的一个因素。报告还描述属性用于下载字体定义。
unicode-range: U+00-FF, U+980-9FF;
⑧“文本”描述属性的特定文字样式,间距和自动滚屏。
text-shadow: #00FFFC 10px 10px 5px; text-transform: capitalize; 常量:capitalize/lowercase/none/uppercase word-wrap: break-word; 常量:break-word/normal -webkit-marquee: right large infinite normal 10s; 常量:direction(方向) increment(迭代次数) repetition(重复) style(样式) speed(速度); -webkit-marquee-direction: ahead/auto/backwards/down/forwards/left/reverse/right/up -webkit-marquee-incrementt: 1-n/infinite(无穷次) -webkit-marquee-speed: fast/normal/slow -webkit-marquee-style: alternate/none/scroll/slide -webkit-text-fill-color: #ff6600; 常量:capitalize, lowercase, none, uppercase -webkit-text-security: circle; 常量:circle/disc/none/square -webkit-text-size-adjust: none; 常量:auto/none; -webkit-text-stroke: 15px #fff; -webkit-line-break: after-white-space; 常量:normal/after-white-space -webkit-appearance: caps-lock-indicator; -webkit-nbsp-mode: space; 常量: normal/space -webkit-rtl-ordering: logical; 常量:visual/logical -webkit-user-drag: element; 常量:element/auto/none -webkit-user-modify: read- only; 常量:read-write-plaintext-only/read-write/read-only -webkit-user-select: text; 常量:text/auto/none
⑨“表格”描述的布局和设计性能表的具体内容。
-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; -webkit-column-break-after: right; 常量:always/auto/avoid/left/right -webkit-column-break-before: right; 常量:always/auto/avoid/left/right –webkit-column-break-inside: logical; 常量:avoid/auto -webkit-column-count: 3; //分栏 -webkit-column-rule: 1px solid #fff; style:dashed,dotted,double,groove,hidden,inset,none,outset,ridge,solid
⑩“用户界面”描述属性,涉及到用户界面元素在浏览器中,如滚动文字区,滚动条,等等。报告还描述属性,范围之外的网页内容,如光标的标注样式和显示当您按住触摸触摸
目标,如在iPhone上的连接。
-webkit-box-align: baseline,center,end,start,stretch 常量:baseline/center/end/start/stretch -webkit-box-direction: normal;常量:normal/reverse -webkit-box-flex: flex_valuet -webkit-box-flex-group: group_number -webkit-box-lines: multiple; 常量:multiple/single -webkit-box-ordinal-group: group_number -webkit-box-orient: block-axis; 常量:block-axis/horizontal/inline-axis/vertical/orientation –webkit-box-pack: alignment; 常量:center/end/justify/start
动画过渡
这是 Webkit 中最具创新力的特性:使用过渡函数定义动画。
-webkit-animation: title infinite ease-in-out 3s; animation 有这几个属性: -webkit-animation-name: //属性名,就是咱们定义的keyframes -webkit-animation-duration:3s //持续时间 -webkit-animation-timing-function: //过渡类型:ease/ linear(线性) /ease-in(慢到快)/ease-out(快到慢) /ease-in-out(慢到快再到慢) /cubic-bezier -webkit-animation-delay:10ms //动画延迟(默认0) -webkit-animation-iteration-count: //循环次数(默认1),infinite 为无限 -webkit-animation-direction: //动画方式:normal(默认 正向播放); alternate(交替方向,第偶数次正向播放,第奇数次反向播放)
这些一样是能够简写的。但真正让我觉的很爽的是keyframes,它能定义一个动画的转变过程供调用,过程为0%到100%或from(0%)到to(100%)。简单点说,只要你有想法,你想让元素在这个过程当中以什么样的方式改变都是很简单的。
-webkit-transform: 类型(缩放scale/旋转rotate/倾斜skew/位移translate) scale(num,num) 放大倍率。scaleX 和 scaleY(3),能够简写为:scale(* , *) rotate(*deg) 转动角度。rotateX 和 rotateY,能够简写为:rotate(* , *) Skew(*deg) 倾斜角度。skewX 和skewY,可简写为:skew(* , *) translate(*,*) 坐标移动。translateX 和translateY,可简写为:translate(* , *)。
###自定义滚动条 from unknown
有没有以为浏览器自带的原始滚动条很不美观,同时也有看到不少网站的自定义滚动条显得高端,就连chrome32.0开发板都抛弃了原始的滚动条,美观多了。那webkit浏览器是如何自定义滚动条的呢?
参考:
后记:有时候,咱们开发H5页面时为了美观,可能会隐藏滚动条,那么此时只要使用以下CSS代码便可实现
::-webkit-scrollbar { width: 0; height: 0; }
<link rel="apple-touch-icon-precomposed" href="http://www.xxx.com/App_icon_114.png" /> <link rel="apple-touch-icon-precomposed" sizes="72x72" href="http://www.xxx.com/App_icon_72.png" /> <link rel="apple-touch-icon-precomposed" sizes="114x114" href="http://www.xxx.com/App_icon_114.png" />
这个属性是当用户把链接保存到手机桌面时使用的图标,若是不设置,则会用网页的截图。有了这,就可让你的网页像APP同样存在手机里了
<link rel="apple-touch-startup-image" href="/img/startup.png" />
这个是APP启动画面图片,用途和上面的相似,若是不设置,启动画面就是白屏,图片像素就是手机全屏的像素
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
这个描述是表示打开的web app的最上面的时间、信号栏是黑色的,固然也能够设置其它参数,详细参数说明请参照:Safari HTML Reference - Supported Meta Tags
<meta name="apple-touch-fullscreen" content="yes" /> <meta name="apple-mobile-web-app-capable" content="yes" />
iPhone 4的一个 CSS 像素实际上表现为一块 2×2 的像素。因此图片像是被放大2倍同样,模糊不清晰。
解决办法:
一、页面引用
<link rel="stylesheet" media="screen and (-webkit-device-pixel-ratio: 0.75)" href="ldpi.css" /> <link rel="stylesheet" media="screen and (-webkit-device-pixel-ratio: 1.0)" href="mdpi.css" /> <link rel="stylesheet" media="screen and (-webkit-device-pixel-ratio: 1.5)" href="hdpi.css" /> <link rel="stylesheet" media="screen and (-webkit-device-pixel-ratio: 2.0)" href="retina.css" />
二、CSS文件里
#header { background:url(mdpi/bg.png); } @media screen and (-webkit-device-pixel-ratio: 1.5) { /*CSS for high-density screens*/ #header { background:url(hdpi/bg.png); } }
16_ms_optimization—web_front-end_performance_optimization
Google AMP (AMP is a way to build web pages for static content that render fast)
对于a标记的点击导航,默认是在onclick事件中处理的。而移动客户端对onclick的响应相比PC浏览器有着明显的几百毫秒延迟。
在移动浏览器中对触摸事件的响应顺序应当是:
ontouchstart -> ontouchmove -> ontouchend -> onclick
所以,若是确实要加快对点击事件的响应,就应当绑定ontouchend事件。
使用click会出现绑定点击区域闪一下的状况,解决:给该元素一个样式以下
-webkit-tap-highlight-color: rgba(0,0,0,0);
若是不使用click,也不能简单的用touchstart或touchend替代,须要用touchstart的模拟一个click事件,而且不能发生touchmove事件,或者用zepto中的tap(轻击)事件。
body { -webkit-overflow-scrolling: touch; }
用iphone或ipad浏览很长的网页滚动时的滑动效果很不错吧?不过若是是一个div,而后设置 height:200px;overflow:auto;
的话,能够滚动可是彻底没有那滑动效果,很郁闷吧?
我看到不少网站为了实现这一效果,用了第三方类库,最经常使用的是iscroll(包括新浪手机页,百度等)
我一开始也使用,不过自从用了-webkit-overflow-scrolling: touch;
样式后,就彻底能够抛弃第三方类库了,把它加在body{}区域,全部的overflow须要滚动的均可以生效了。
另外有一篇比较全的移动端点击解决方案 http://www.zhihu.com/question/28979857
ontouchmove="event.preventDefault()" //锁定viewport,任何屏幕操做不移动用户界面(弹出键盘除外)。
Media Query 相信大部分人已经使用过了。其实 JavaScript能够配合 Media Query这么用:
var mql = window.matchMedia("(orientation: portrait)"); mql.addListener(handleOrientationChange); handleOrientationChange(mql); function handleOrientationChange(mql) { if (mql.matches) { alert('The device is currently in portrait orientation ') } else { alert('The device is currently in landscape orientation') }}
借助了 Media Query 接口作的事件监听,因此很强大!
也能够经过获取 CSS 值来使用 Media Query 判断设备状况,详情请看:JavaScript 依据 CSS Media Queries 判断设备的方法。
rem是很是好用的一个属性,能够根据html来设定基准值,并且兼容性也很不错。不过有的时候仍是须要对一些莫名其妙的浏览器优雅降级。如下是两个实践
能够用如下的代码片断保证在低端浏览器下也不会出问题
html { font-size: 62.5%; }
body { font-size: 14px; font-size: 1.4rem; } /* =14px /
h1 { font-size: 24px; font-size: 2.4rem; } / =24px */
-webkit-tap-highlight-color: 颜色
开发特定设备的移动网站,首先要作的就是设备侦测了。下面是使用Javascript侦测iPhone/iPod的UA,而后转向到专属的URL。
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) { if (document.cookie.indexOf("iphone_redirect=false") == -1) { window.location = "http://m.example.com"; } }
android上,当软键盘状态改变的时候,会触发window的resize事件,因此咱们能够进入页面的时候保存一次window.innerHeight的值,当window的resize事件触发的时候,比较window.innerHeight的值与前一次保存的window.innerHeight的值大小来判断软键盘的收拢和弹出状态。
var winHeight = window.innerHeight;
if (isAndroid) {
window.addEventListener('resize', function(e) {
var tempHeight = window.innerHeight
if (tempHeight < winHeight) {
bShowRec = false;
} else {
bShowRec = true;
}
});
}
ios上,软键盘状态改变的时候,不会触发window的resize事件,可是当软键盘的“完成”按钮被点击的时候,会触发onblur事件。因此正常经过onfocus和onblur事件来判断就行。
虽然Javascript是能够在水果设备上运行的,可是用户仍是能够禁用。它也会形成客户端刷新和额外的数据传输,因此下面是服务器端侦测和转向:
if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod')) { header('Location: http://yoursite.com/iphone'); exit(); }
html, body, form, fieldset, p, div, h1, h2, h3, h4, h5, h6 {-webkit-text-size-adjust:none;}
document.body.ontouchmove=function(e){ e.preventDefault(); }
<http://blog.csdn.net/freshlover/article/details/40432247>
由于iPhone并无鼠标指针,因此没有hover事件。那么CSS :hover伪类就没用了。可是iPhone有Touch事件,onTouchStart 相似 onMouseOver,onTouchEnd 相似 onMouseOut。因此咱们能够用它来模拟hover。使用Javascript:
var myLinks = document.getElementsByTagName('a'); for(var i = 0; i < myLinks.length; i++){ myLinks[i].addEventListener(’touchstart’, function(){this.className = “hover”;}, false); myLinks[i].addEventListener(’touchend’, function(){this.className = “”;}, false); }
而后用CSS增长hover效果:
a:hover, a.hover { /* 你的hover效果 */ }
这样设计一个连接,感受能够更像按钮。而且,这个模拟能够用在任何元素上。
http://www.w3.org/TR/css3-flexbox/
居中是移动端跟pc端共同的噩梦。这里有两种兼容性比较好的新方案。
table布局法
.box{
text-align:center;
display:table-cell;
vertical-align:middle;
}
老版本flex布局法
.box{
display:-webkit-box;
-webkit-box-pack: center;
-webkit-box-align: center;
text-align:center;
}
以上两种其实分别是retchat跟ionic的布局基石。
这里有更详细的更多的选择http://www.zhouwenbin.com/%E5%9E%82%E7%9B%B4%E5%B1%85%E4%B8%AD%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E6%B3%95/ 来自周文彬的博客
h5页面有个很蛋疼的问题就是,当输入框在最底部,点击软键盘后输入框会被遮挡。
可使用这个api,在点击input的时候调用便可 https://developer.mozilla.org/zh-CN/docs/Web/API/Element/scrollIntoView
若是切换输入法,因为不一样输入法高度不一样,又会出现被遮挡问题。因为没法捕获切换输入法的事件,所以能够开一个计时器,不断执行sscrollintoview便可。
http://www.75team.com/archives/611
input 的placeholder会出现文本位置偏上的状况:PC端设置line-height等于height可以对齐,而移动端仍然是偏上,解决是设置line-height:normal,(stackoverflow也可查到这种解决办法)。
(经典)Using CSS Sprites to optimize your website for Retina Displays
使用CSS3的background-size优化苹果的Retina屏幕的图像显示
使用 CSS sprites 来优化你的网站在 Retina 屏幕下显示
(案例)CSS IMAGE SPRITES FOR RETINA (HIRES) DEVICES
这实际上是浏览器本身的处理。由于浏览器会针对此类型 input 增长 datepicker 模块。
对 input type date 使用 placeholder 的目的是为了让用户更准确的输入日期格式,iOS 上会有 datepicker 不会显示 placeholder 文字,可是为了统一表单外观,每每须要显示。Android 部分机型没有 datepicker 也不会显示 placeholder 文字。
桌面端(Mac)
移动端
解决方法:
<input placeholder="Date" class="textbox-n" type="text" onfocus="(this.type='date')" id="date">
由于text是支持placeholder的。所以当用户focus的时候自动把type类型改变为date,这样既有placeholder也有datepicker了
有这样一种需求,须要判断用户照片是横着拍出来的仍是竖着拍出来的,这里须要使用照片得exif信息:
$("input").change(function() { var file = this.files[0]; fr = new FileReader; fr.onloadend = function() { var exif = EXIF.readFromBinaryFile(new BinaryFile(this.result)); alert(exif.Orientation); }; fr.readAsBinaryString(file); });
可使用这两个库 来取exif信息http://www.nihilogic.dk/labs/binaryajax/binaryajax.js http://www.nihilogic.dk/labs/exif/exif.js
http://www.iunbug.com/archives/2013/04/23/798.html
当前不少无线页面都使用前端模板进行数据渲染,那么在糟糕的网速状况下,一进去页面,看到的不是白屏就是 loading,这成为白屏问题。
此问题发生的缘由基本能够归结为网速跟静态资源
一、css文件加载须要一些时间,在加载的过程当中页面是空白的。 解决:能够考虑将css代码前置和内联。
二、首屏无实际的数据内容,等待异步加载数据再渲染页面致使白屏。 解决:在首屏直接同步渲染html,后续的滚屏等再采用异步请求数据和渲染html。
三、首屏内联js的执行会阻塞页面的渲染。 解决:尽可能不在首屏html代码中放置内联脚本。(来自翔歌)
解决方案
根本缘由是客户端渲染的无力,所以最简单的方法是在服务器端,使用模板引擎渲染全部页面。同时
1减小文件加载体积,如html压缩,js压缩
2加快js执行速度 好比常见的无限滚动的页面,可使用js先渲染一个屏幕范围内的东西
3提供一些友好的交互,好比提供一些假的滚动条
4使用本地存储处理静态文件。
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion',deviceMotionHandler, false);
}
var speed = 30;//speed
var x = y = z = lastX = lastY = lastZ = 0;
function deviceMotionHandler(eventData) {
var acceleration =event.accelerationIncludingGravity;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
if(Math.abs(x-lastX) > speed || Math.abs(y-lastY) > speed || Math.abs(z-lastZ) > speed) {
alert('别摇那么大力嘛...');
// your code here
}
lastX = x;
lastY = y;
lastZ = z;
}
来自 http://gallery.kissyui.com/redirectToNative/1.2/guide/index.html kissy mobile
经过iframe src发送请求打开app自定义url scheme,如taobao://home(淘宝首页) 、etao://scan(一淘扫描));
若是安装了客户端则会直接唤起,直接唤起后,以前浏览器窗口(或者扫码工具的webview)推入后台;
若是在指定的时间内客户端没有被唤起,则js重定向到app下载地址。
大概实现代码以下
goToNative:function(){ if(!body) { setTimeout(function(){ doc.body.appendChild(iframe); }, 0); } else { body.appendChild(iframe); } setTimeout(function() { doc.body.removeChild(iframe); gotoDownload(startTime);//去下载,下载连接通常是itunes app store或者apk文件连接 /** * 测试时间设置小于800ms时,在android下的UC浏览器会打开native app时并下载apk, * 测试android+UC下打开native的时间最好大于800ms; */ }, 800); }
须要注意的是 若是是android chrome 25版本之后,在iframe src不会发送请求,
缘由以下https://developers.google.com/chrome/mobile/docs/intents ,经过location href使用intent机制拉起客户端可行而且当前页面不跳转。
window.location = 'intent://' + schemeUrl + '#Intent;scheme=' + scheme + ';package=' + self.package + ';end';
补充一个来自三水清的详细讲解 http://js8.in/2013/12/16/ios%E4%BD%BF%E7%94%A8schema%E5%8D%8F%E8%AE%AE%E8%B0%83%E8%B5%B7app/
今天发现,要让a连接的CSS active伪类生效,只须要给这个a连接的touch系列的任意事件touchstart/touchend绑定一个空的匿名方法便可hack成功
<style> a { color: #000; } a:active { color: #fff; } </style> <a herf=”asdasd”>asdasd</a> <script> var a=document.getElementsByTagName(‘a’); for(var i=0;i<a.length;i++){ a[i].addEventListener(‘touchstart’,function(){},false); } </script>
两个方法:使用css3动画的时尽可能利用3D加速,从而使得动画变得流畅。动画过程当中的动画闪白能够经过 backface-visibility 隐藏。
-webkit-transform-style: preserve-3d; /*设置内嵌的元素在 3D 空间如何呈现:保留 3D*/ -webkit-backface-visibility: hidden; /*(设置进行转换的元素的背面在面对用户时是否可见:隐藏)*/
document.implementation.hasFeature("http:// www.w3.org/TR/SVG11/feature#Image", "1.1")
ios的safari提供一种“隐私模式”,若是你的webapp考虑兼容这个模式,那么在使用html5的本地存储的一种————localStorage时,可能由于“隐私模式”下没有权限读写localstorge而使代码抛出错误,致使后续的js代码都没法运行了。
既然在safari的“隐私模式”下,没有调用localStorage的权限,首先想到的是先判断是否支持localStorage,代码以下:
if('localStorage' in window){ //须要使用localStorage的代码写在这 }else{ //不支持的提示和向下兼容代码 }
测试发现,即便在safari的“隐私模式”下,’localStorage’ in window的返回值依然为true,也就是说,if代码块内部的代码依然会运行,问题没有获得解决。
接下来只能至关使用try catch了,虽然这是一个不太推荐被使用的方法,使用try catch捕获错误,使后续的js代码能够继续运行,代码以下:
try{ if('localStorage' in window){ //须要使用localStorage的代码写在这 }else{ //不支持的提示和向下兼容代码 } }catch(e){ // 隐私模式相关提示代码和不支持的提示和向下兼容代码 }
因此,提醒你们注意,在须要兼容ios的safari的“隐私模式”的状况下,本地存储相关的代码须要使用try catch包裹并降级兼容。
有些安卓手机,页面点击时会中止页面的javascript,css3动画等的执行,这个比较蛋疼。不过能够用阻止默认事件解决。详细见
http://stackoverflow.com/questions/10246305/android-browser-touch-events-stop-display-being-updated-inc-canvas-elements-h
function touchHandlerDummy(e) { e.preventDefault(); return false; } document.addEventListener("touchstart", touchHandlerDummy, false); document.addEventListener("touchmove", touchHandlerDummy, false); document.addEventListener("touchend", touchHandlerDummy, false);
input:-ms-clear{display:none;}
UIWebView font is thinner in portrait than landscape
navigator.standalone
// 隐藏地址栏 & 处理事件的时候 ,防止滚动条出现 addEventListener('load', function(){ setTimeout(function(){ window.scrollTo(0, 1); }, 100); });
http://darkblue.sdf.org/weblog/ios-7-dot-1-mobile-safari-minimal-ui.html
// 判断是否为 iPhone : function isAppleMobile() { return (navigator.platform.indexOf('iPhone') != -1); };
var v = localStorage.getItem('n') ? localStorage.getItem('n') : ""; // 若是名称是 n 的数据存在 ,则将其读出 ,赋予变量 v 。 localStorage.setItem('n', v); // 写入名称为 n、值为 v 的数据 localStorage.removeItem('n'); // 删除名称为 n 的数据
若是你关闭自动识别后 ,又但愿某些电话号码可以连接到 iPhone 的拨号功能 ,那么能够经过这样来声明电话连接 ,
<a href="tel:12345654321">打电话给我</a> <a href="sms:12345654321">发短信</a>
或用于单元格:
<td onclick="location.href='tel:122'">
要关闭这两项功能,能够经过autocapitalize 与autocorrect 这两个选项:
<input type="text" autocapitalize="off" autocorrect="off" />
<meta content="email=no" name="format-detection" />
-webkit-touch-callout:none
-webkit-user-select:none
Why Moving Elements With Translate() Is Better Than Pos:abs Top/left
window.scrollY window.scrollX
好比要绑定一个touchmove的事件,正常的状况下相似这样(来自呼吸二氧化碳)
$('div').on('touchmove', function(){ //.….code {});
而若是中间的code须要处理的东西多的话,fps就会降低影响程序顺滑度,而若是改为这样
$('div').on('touchmove', function(){ setTimeout(function(){ //.….code },0); {});
把代码放在setTimeout中,会发现程序变快.
http://www.zhouwenbin.com/positionsticky-%E7%B2%98%E6%80%A7%E5%B8%83%E5%B1%80/
http://www.zhouwenbin.com/sticky%E6%A8%A1%E6%8B%9F%E9%97%AE%E9%A2%98/
能够经过正则去掉
this.value = this.value.replace(/\u2006/g, '');
见下图
Android Web 视图,至少在 HTC EVO 和三星的 Galaxy Nexus 中,文本输入框在输入时表现的就像占位符。状况为一个相似水印的东西在用户输入区域,一旦用户开始输入便会消失(见图片)。
在 Android 的默认样式下当输入框得到焦点后,若存在一个绝对定位或者 fixed 的元素,布局会被破坏,其余元素与系统输入字段会发生重叠(如搜索图标将消失为搜索字段),能够观察到布局与原始输入字段有误差(见截图)。
这是一个至关复杂的问题,如下简单布局能够重现这个问题:
<label for="phone">Phone: *</label> <input type="tel" name="phone" id="phone" minlength="10" maxlength="10" inputmode="latin digits" required="required" />
解决方法
-webkit-user-modify: read-write-plaintext-only
详细参考http://www.bielousov.com/2012/android-label-text-appears-in-input-field-as-a-placeholder/
注意,该属性会致使中文不能输入词组,只能单个字。感谢鬼哥与飞(游勇飞)贡献此问题与解决方案
另外,在position:fixed后的元素里,尽可能不要使用输入框。更多的bug可参考
http://www.cosdiv.com/page/M0/S882/882353.html
依旧没法解决(摩托罗拉ME863手机),则使用input:text类型而非password类型,并设置其设置 -webkit-text-security: disc; 隐藏输入密码从而解决。
解决方法删除了overflow-x:hidden; 而后在JS生成下来菜单以后focus聚焦,这两步操做以后解决了问题。(来自岛都-小Qi)
input::-webkit-input-speech-button {display: none}
Mobile-friendly input of a digits + spaces string (a credit card number)
HTML5 input type number vs tel
iPhone: numeric keyboard for text input
Text Programming Guide for iOS - Managing the Keyboard
HTML5 inputs and attribute support
点击浏览器的回退,有时候不会自动执行js,特别是在mobilesafari中。这与往返缓存(bfcache)有关系。有不少hack的处理方法,能够参考
http://stackoverflow.com/questions/24046/the-safari-back-button-problem
http://stackoverflow.com/questions/11979156/mobile-safari-back-button
https://www.imququ.com/post/ios-none-freeze-timer.html
或者能够用postmessage方式:
主页面:
// 解决ios safari tab在后台会遭遇进程冻结问题 // http://www.apple.com/safari/#gallery-icloud-tabs // Safari takes advantage of power-saving technologies such as App Nap, which puts background Safari tabs into a low-power state until you start using them again. In addition, Safari Power Saver conserves battery life by intelligently pausing web videos and other plug‑in content when they’re not front and center on the web pages you visit. All told, Safari on OS X Mavericks lets you browse up to an hour longer than with Chrome or Firefox.1 var work; function startWorker() { if (typeof(Worker) !== "undefined") { if (typeof(work) == "undefined") { work = new Worker("/workers.js"); } work.onmessage = function(event) { // document.getElementById("result-count").innerHTML = event.data.count; // document.getElementById("result-url").innerHTML = event.data.targetURL; if (target && event.data.targetURL != "") target.location.href = event.data.targetURL; }; } else { console.log('does not support Web Workers...'); } } function stopWorker() { work.terminate(); } startWorker();
worker:
// 解决ios safari tab在后台会遭遇进程冻结问题 // http://www.apple.com/safari/#gallery-icloud-tabs // Safari takes advantage of power-saving technologies such as App Nap, which puts background Safari tabs into a low-power state until you start using them again. In addition, Safari Power Saver conserves battery life by intelligently pausing web videos and other plug‑in content when they’re not front and center on the web pages you visit. All told, Safari on OS X Mavericks lets you browse up to an hour longer than with Chrome or Firefox.1 importScripts('/socket.io/socket.io.js'); var count = 0, targetURL = '' ; var socket = io.connect('/'); socket.on('navigate', function (data) { count = count++; postMessage({targetURL:data.url,count:count}); });
http://efe.baidu.com/blog/mobile-fixed-layout/
参考 http://stackoverflow.com/questions/20443574/fixed-background-image-with-ios7
<audio autoplay ><source src="audio/alarm1.mp3" type="audio/mpeg"></audio>
系统默认状况下 audio的autoplay属性是没法生效的,这也是手机为节省用户流量作的考虑。
若是必需要自动播放,有两种方式能够解决。
1.捕捉一次用户输入后,让音频加载,下次便可播放。
//play and pause it once document.addEventListener('touchstart', function () { document.getElementsByTagName('audio')[0].play(); document.getElementsByTagName('audio')[0].pause(); });
这种方法须要捕获一次用户的点击事件来促使音频跟视频加载。当加载后,你就能够用javascript控制音频的播放了,如调用audio.play()
2.利用iframe加载资源
var ifr=document.createElement("iframe"); ifr.setAttribute('src', "http://mysite.com/myvideo.mp4"); ifr.setAttribute('width', '1px'); ifr.setAttribute('height', '1px'); ifr.setAttribute('scrolling', 'no'); ifr.style.border="0px"; document.body.appendChild(ifr);
这种方式其实跟第一种原理是同样的。当资源加载了你就能够控制播放了,可是这里使用iframe来加载,至关于直接触发资源加载。
注意,使用建立audio标签并让其加载的方式是不可行的。
慎用这种方法,会对用户形成很糟糕的影响。。
@media (device-height: 568px) and (-webkit-min-device-pixel-ratio: 2) { /* iPhone 5 or iPod Touch 5th generation */ }
<link href="startup-568h.png" rel="apple-touch-startup-image" media="(device-height: 568px)"> <link href="startup.png" rel="apple-touch-startup-image" sizes="640x920" media="(device-height: 480px)">
<input type=file accept="video/*"> <input type=file accept="image/*">
不支持其余类型的文件 ,如音频,Pages文档或PDF文件。 也没有getUserMedia摄像头的实时流媒体支持。
有了智能应用程序横幅,当网站上有一个相关联的本机应用程序时,Safari浏览器能够显示一个横幅。 若是用户没有安装这个应用程序将显示“安装”按钮,或已经安装的显示“查看”按钮可打开它。
在 iTunes Link Maker 搜索咱们的应用程序和应用程序ID。
<meta name="apple-itunes-app" content="app-id=9999999">
可使用 app-argument 提供字符串值,若是参加iTunes联盟计划,能够添加元标记数据
<meta name="apple-itunes-app" content="app-id=9999999, app-argument=xxxxxx"> <meta name="apple-itunes-app" content="app-id=9999999, app-argument=xxxxxx, affiliate-data=partnerId=99&siteID=XXXX">
横幅须要156像素(设备是312 hi-dpi)在顶部,直到用户在下方点击内容或关闭按钮,你的网站才会展示所有的高度。 它就像HTML的DOM对象,但它不是一个真正的DOM。
CSS3 滤镜
-webkit-filter: blur(5px) grayscale (.5) opacity(0.66) hue-rotate(100deg);
交叉淡变
background-image: -webkit-cross-fade(url("logo1.png"), url("logo2.png"), 50%);
Safari中的全屏幕
除了chrome-less 主屏幕meta标签,如今的iPhone和iPod Touch(而不是在iPad)支持全屏幕模式的窗口。 没有办法强制全屏模式,它须要由用户启动(工具栏上的最后一个图标)。须要引导用户按下屏幕上的全屏图标来激活全屏效果。 可使用onresize事件检测是否用户切换到全屏幕。
支持requestAnimationFrameAPI
支持image-set,retina屏幕的利器
-webkit-image-set(url(low.png) 1x, url(hi.jpg) 2x)
应用程序缓存限制增长至25MB。
Web View(pseudobrowsers,PhoneGap/Cordova应用程序,嵌入式浏览器) 上Javascript运行比Safari慢3.3倍(或者说,Nitro引擎在Safari浏览器是Web应用程序是3.3倍速度)。
autocomplete属性的输入遵循DOM规范
来自DOM4的Mutation Observers已经实现。 您可使用WebKitMutationObserver构造器捕获DOM的变化
Safari再也不老是对用 -webkit-transform:preserve-3d 的元素建立硬件加速
支持window.selection 的Selection API
Canvas更新 :createImageData有一个参数,如今有两个新的功能作好准备,用webkitGetImageDataHD和webkitPutImageDataHD提供高分辨率图像 。
更新SVG处理器和事件构造函数
iOS 7 的 Safari 和 HTML5:问题,变化和新 API(张金龙翻译)
browser.getSettings().setAppCacheEnabled(true); browser.getSettings().setAppCachePath("/data/data/[com.packagename]/cache"); browser.getSettings().setAppCacheMaxSize(5*1024*1024); // 5MB
browser.getSettings().setDatabaseEnabled(true); browser.getSettings().setDomStorageEnabled(true); String databasePath = browser.getContext().getDir("databases", Context.MODE_PRIVATE).getPath(); browser.getSettings().setDatabasePath(databasePath);//Android webview的LocalStorage有个问题,关闭APP或者重启后,就清楚了,因此须要browser.getSettings().setDatabase相关的操做,把LocalStoarge存到DB中 myWebView.setWebChromeClient(new WebChromeClient(){ @Override public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { quotaUpdater.updateQuota(estimatedSize * 2); } }
browser.getSettings().setBuiltInZoomControls(false);
使用localstorage缓存html
使用lazyload,还要记得lazyload占位图虽然小,可是最好能提早加载到缓存
延时加载执行js
主要缘由就在于Android Webview的onPageFinished事件,Android端通常是用这个事件来标识页面加载完成并显示的,也就是说在此以前,会一直loading,可是Android的OnPageFinished事件会在Javascript脚本执行完成以后才会触发。若是在页面中使用JQuery,会在处理完DOM对象,执行完$(document).ready(function() {});事件自会后才会渲染并显示页面。
http://www.alloyteam.com/2013/12/web-cache-6-hybrid-app-tailored-cache/
相关解决方案
http://mt.tencent.com/
在PC上,咱们能够很方便地配host,可是手机上如何配host,这是一个问题。
这里主要使用fiddler和远程代理,实现手机配host的操做,具体操做以下:
首先,保证PC和移动设备在同一个局域网下;
PC上开启fiddler,并在设置中勾选“allow remote computers to connect”
首先,保证PC和移动设备在同一个局域网下;
PC上开启fiddler,并在设置中勾选“allow remote computers to connect”
手机上设置代理,代理IP为PC的IP地址,端口为8888(这是fiddler的默认端口)。一般手机上能够直接设置代理,若是没有,能够去下载一个叫ProxyDroid的APP来实现代理的设置。
此时你会发现,用手机上网,走的实际上是PC上的fiddler,全部的请求包都会在fiddler中列出来,配合willow使用,便可实现配host,甚至是反向代理的操做。
也能够用CCProxy之类软件,还有一种方法就是买一个随身wifi,而后手机链接就能够了!
iPhone上使用Burp Suite捕捉HTTPS通讯包方法
mobile app 通讯分析方法小议(iOS/Android)
实时抓取移动设备上的通讯包(ADVsock2pipe+Wireshark+nc+tcpdump)
通常用代理软件代理过来的静态资源能够设置nocache避免缓存,可是有的手机比较诡异,会一直缓存住css等资源文件。因为静态资源通常都是用版本号管理的,咱们以charles为例子来处理这个问题
charles 选择静态的html页面文件-saveResponse。以后把这个文件保存一下,修改一下版本号。以后继续发请求,
刚才的html页面文件 右键选择 --map local 选择咱们修改过版本号的html文件即ok。这其实也是fiddler远程映射并修改文件的一个应用场景。
http://www.farsight.com.cn/news/emb105.htm
http://testerhome.com/topics/388
http://www.cnblogs.com/zdz8207/archive/2012/01/30/2332436.html
微信浏览器的调试技巧http://www.html-js.com/article/WeChat-cock-burst-perfect-debugging-WeChat-WebView-x5%203076
微信浏览器的各类bug汇总 (x5内核) http://www.qianduan.net/qqliu-lan-qi-x5nei-he-wen-ti-hui-zong/
由于微信浏览器屏蔽了一部分连接图片,因此须要引导用户去打开新页面,能够用如下方式判断微信浏览器的ua
function is_weixn(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger/i)=="micromessenger") { return true; } else { return false; } }
后端判断也很简单,好比php
function is_weixin(){ if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) { return true; } return false; }
https://github.com/maxzhang/maxzhang.github.com/issues/31 微信浏览器踩坑,来自maxZhang https://github.com/maxzhang
场景:
测试环境:UC浏览器 8.7/8.6 + Android 2.3/4.0 。
Demo:http://t.cn/zj3xiyu
解决方案:不使用transform属性。translate用top、margin等属性替代。
场景:
测试环境:UC浏览器 8.7/8.6 + Android 2.3/4.0 。
场景:设置了position: fixed 的元素会遮挡z-index值更高的同辈元素。
在8.6的版本,这个状况直接出现。
在8.7以后的版本,当同辈元素的height大于713这个「神奇」的数值时,才会被遮挡。
测试环境:UC浏览器 8.8_beta/8.7/8.6 + Android 2.3/4.0 。
Demo:http://t.cn/zYLTSg6
场景:使用如下代码,横竖屏操做后,rem并无被从新计算,一开始觉得是页面没有重绘,强制重绘页面后,发现问题并无解决。
(function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize', recalc = function () { var clientWidth = docEl.clientWidth; if (!clientWidth) return; docEl.style.fontSize = 100 * (clientWidth / 320) + 'px'; }; recalc(); if (!doc.addEventListener) return; win.addEventListener(resizeEvt, recalc, false); doc.addEventListener('DOMContentLoaded', recalc, false); })(document, window);
测试环境:UC浏览器 V10.9 + Android 6.0+ 。
解决方案:手动在head中插入style,给html设置font-size,并使用 !important 增长优先级,
(function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize', recalc = function () { var clientWidth = docEl.clientWidth; if (!clientWidth) return; var style; if(style=document.getElementById("hackUcRem")){ style.parentNode.removeChild(style); } style = document.createElement("style"); style.id="hackUcRem"; document.head.appendChild(style); style.appendChild(document.createTextNode("html{font-size:" + 100 * (clientWidth / 320) + "px !important;}")); docEl.style.fontSize = 100 * (clientWidth / 320) + 'px'; }; recalc(); if (!doc.addEventListener) return; win.addEventListener(resizeEvt, recalc, false); doc.addEventListener('DOMContentLoaded', recalc, false); })(document, window);
场景:带有HttpOnly属性的Cookie,在QQ手机浏览器版本从4.0开始失效。JavaScript能够直接读取设置了HttpOnly的Cookie值。
测试环境:QQ手机浏览器 4.0/4.1/4.2 + Android 4.0 。
场景:location.hash 被赋值后,地址栏的地址不会改变。
但实际上 location.href 已经更新了,经过JavaScript能够顺利获取到更新后的地址。
虽然不影响正常访问,但用户没法将访问过程当中改变hash后的地址存为书签。
测试环境:MIUI 4.0
场景:父元素设置position: fixed;
子元素设置position: absolute;
此时,若是父元素/子元素还设置了overflow: hidden 则出现“父元素遮挡该子元素“的bug。
视觉(view)层并无出现遮挡,只是没法触发绑定在该子元素上的事件。可理解为:「看到点不到」。
补充: 页面往下滚动,触发position: fixed;的特性时,才会出现这个bug,在最顶不会出现。
测试平台: 小米1S,Android4.0的Chrome18
demo: http://maplejan.sinaapp.com/demo/fixed_chromemobile.html
解决办法: 把父元素和子元素的overflow: hidden去掉。
以上来源于 http://www.cnblogs.com/maplejan/archive/2013/04/26/3045928.html
https://github.com/madrobby/zepto/blob/master/src/touch.js 去掉61行,其实就是使用原生的滚动
iscroll4 的几个bug(来自 http://www.mansonchor.com/blog/blog_detail_64.html 内有详细讲解)
1.滚动容器点击input框、select等表单元素时没有响应】
onBeforeScrollStart: function (e) { e.preventDefault(); }
改成
onBeforeScrollStart: function (e) { var nodeType = e.explicitOriginalTarget © e.explicitOriginalTarget.nodeName.toLowerCase():(e.target © e.target.nodeName.toLowerCase():'');if(nodeType !='select'&& nodeType !='option'&& nodeType !='input'&& nodeType!='textarea') e.preventDefault(); }
2.往iscroll容器内添加内容时,容器闪动的bug
源代码的
has3d = 'WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix()
改为
has3d = false
在配置iscroll时,useTransition设置成false
3.过长的滚动内容,致使卡顿和app直接闪退
4.左右滚动时,不能正确响应正文上下拉动
iscroll的闪动问题也与渲染有关系,能够参考
运用webkit绘制渲染页面原理解决iscroll4闪动的问题
iscroll4升级到5要注意的问题
可使用如下的解决方案(利用data-setapi)
<a ontouchmove="this.s=1" ontouchend="this.s || window.open(this.dataset.href),this.s=0" target="_blank" data-href="http://www.hao123.com/topic/pig">黄浦江死猪之谜</a>
也能够用这种方法
$(document).delegate('[data-target]', 'touchmove', function () { $(this).attr('moving','moving'); }) $(document).delegate('[data-target]', 'touchend', function () { if ($(this).attr('moving') !== 'moving') { //作你想作的。。 $(this).attr('moving', 'notMoving'); } else { $(this).attr('moving', 'notMoving'); } })
知乎专栏 - [无线手册-4] dp、sp、px傻傻分不清楚[完整]
Resolution Independent Mobile UI
Pixel density, retina display and font-size in CSS
手机浏览器也是浏览器,在ajax调用外部api的时候也存在跨域问题。固然利用 PhoneGap 打包后,因为协议不同就不存在跨域问题了。
但页面一般是须要跟后端进行调试的。通常会报相似
XMLHttpRequest cannot load XXX Origin null is not allowed by Access-Control-Allow-Origin.
以及
XMLHttpRequest cannot load http://. Request header field Content-Type is not allowed by Access-Control-Allow-Headers."
这时候可让后端加上两个http头
Access-Control-Allow-Origin "*" Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
第一个头能够避免跨域问题,第二个头能够方便ajax请求设置content-type等配置项
这个会存在一些安全问题,能够参考这个问题的讨论 http://www.zhihu.com/question/22992229
http://snoopyxdy.blog.163.com/blog/static/60117440201432491123551 这里有一大堆snoopy总结的phonggap开发坑
在 Android 项目中的 assets 中的 HTML 页面中加入如下代码,即可解决问题
window,html,body{ overflow-x:hidden !important; -webkit-overflow-scrolling: touch !important; overflow: scroll !important; }
参考:
出现这个问题多是由于 Navigator 取 contacts 时绑定的 window.onload
注意使用 PhoneGap 的 API 时,必定要在 devicereay 事件的处理函数中使用 API
document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { callFetchContacts(); } function callFetchContacts(){ var options = new ContactFindOptions(); options.multiple = true; var fields = ["displayName", "name","phoneNumbers"]; navigator.contacts.find(fields, onSuccess, onError,options); }