HTML5移动开发技术要点总结及各事件含义

来源:互联网 发布:微信企业宣传制作软件 编辑:程序博客网 时间:2024/04/29 23:01
如果你是一名前端er,又想在移动设备上开发出自己的应用,那怎么实现呢?幸好,webkit内核的浏览器能帮助我们完成这一切。接触 webkit webApp的开发已经有一段时间了,现把一些技巧分享给大家 : 

  • 1. viewport:

也就是可视区域。对于桌面浏览器,我们都很清楚viewport是什么,就是出去了所有工具栏、状态栏、滚动条等等之后用于看网页的区域, 

这是真正有效的区域。由于移动设备屏幕宽度不同于传统web,因此我们需要改变viewport; 

实际上我们可以操作的属性有4 个: 
Java代码  收藏代码
  1. width -             //  viewport 的宽度 (范围从200 到10,000,默认为980 像素)  
  2.   
  3. height -            //  viewport 的高度 (范围从223 到10,000)  
  4.   
  5. initial-scale -     //  初始的缩放比例 (范围从>0 到10)  
  6.   
  7. minimum-scale -    //   允许用户缩放到的最小比例  
  8.   
  9. maximum-scale -    //   允许用户缩放到的最大比例  
  10.   
  11. user-scalable -    //   用户是否可以手动缩 (no,yes)  


那么到底这些设置如何让Safari 知道?其实很简单,就一个meta,形如: 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">   //编码 
<meta id="viewport" name="viewport" content="width=320; initial-scale=1.0;maximum-scale=1.0; user-scalable=no;"/> 
<meta name=”apple-mobile-web-app-capable” content=”yes” />  // 离线应用的另一个技巧    
<meta name=”apple-mobile-web-app-status-bar-style” content=black” />  // 隐藏状态栏       
<meta content="black" name="apple-mobile-web-app-status-bar-style" /> //指定的iphone中safari顶端的状态条的样式       
<meta content="telephone=no" name="format-detection" />       //告诉设备忽略将页面中的数字识别为电话号码     
<meta name="Author" contect="Mr.He"/ > 

在设置了initial-scale=1 之后,我们终于可以以1:1 的比例进行页面设计了。关viewport,还有一个很重要的概念是:iphone 的safari 浏览器完全没有滚动条,而且不是简单的“隐藏滚动条”,是根本没有这个功能。iphone 的safari 浏览器实际上从一开始就完整显示了这个网页,然后用viewport 查看其中的一部分。当你用手指拖动时,其实拖的不是页面,而是viewport。浏览器行为的改变不止是滚动条,交互事件也跟普通桌面不一样。(请参考:指尖的下JS 系列文章) 

  • 2. link:

Java代码  收藏代码
  1. <link rel=”apple-touch-startup-image” href=”startup.png” /> // 设置开始页面图片  
  2. <link rel=”apple-touch-icon” href=”iphon_tetris_icon.png”/> // 在设置书签的时候可以显示好看的图标  
  3. <link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">    // 肖像模式样式        
  4. <link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css"   // 风景模式样式  
  5. //竖屏时使用的样式  
  6. <style media="all and (orientation:portrait)" type="text/css">  
  7. #landscape { display: none; }  
  8. </style>  
  9. //横屏时使用的样式  
  10. <style media="all and (orientation:landscape)" type="text/css">  
  11. #portrait { display: none; }  
  12. </style>   


  • 3. 事件 : (请参考:指尖的下JS 系列文章)

// 手势事件 
touchstart            //当手指接触屏幕时触发 

touchmove           //当已经接触屏幕的手指开始移动后触发 

touchend             //当手指离开屏幕时触发 

touchcancel 

// 触摸事件 
gesturestart          //当两个手指接触屏幕时触发 

gesturechange      //当两个手指接触屏幕后开始移动时触发 

gestureend 

// 屏幕旋转事件  
onorientationchange    

// 检测触摸屏幕的手指何时改变方向      

orientationchange      

// touch事件支持的相关属性 
touches        

targetTouches      

changedTouches             

clientX    // X coordinate of touch relative to the viewport (excludes scroll offset)      

clientY    // Y coordinate of touch relative to the viewport (excludes scroll offset)      

screenX    // Relative to the screen       

screenY     // Relative to the screen      

pageX     // Relative to the full page (includes scrolling)    

pageY     // Relative to the full page (includes scrolling)    

target     // Node the touch event originated from     

identifier     // An identifying number, unique to each touch event 

  • 4. 屏幕旋转事件:onorientationchange

添加屏幕旋转事件侦听,可随时发现屏幕旋转状态(左旋、右旋还是没旋)。例子: 

Java代码  收藏代码
  1. // 判断屏幕是否旋转  
  2. function orientationChange() {  
  3.   
  4.     switch(window.orientation) {  
  5.   
  6.       case 0:   
  7.   
  8.             alert("肖像模式 0,screen-width: " + screen.width + "; screen-height:" + screen.height);  
  9.   
  10.             break;  
  11.   
  12.       case -90:   
  13.   
  14.             alert("左旋 -90,screen-width: " + screen.width + "; screen-height:" + screen.height);  
  15.   
  16.             break;  
  17.   
  18.       case 90:     
  19.   
  20.             alert("右旋 90,screen-width: " + screen.width + "; screen-height:" + screen.height);  
  21.   
  22.             break;  
  23.   
  24.       case 180:     
  25.   
  26.           alert("风景模式 180,screen-width: " + screen.width + "; screen-height:" + screen.height);  
  27.   
  28.           break;  
  29.   
  30.     };<br>}  


Java代码  收藏代码
  1. // 添加事件监听  
  2. addEventListener('load', function(){  
  3.   
  4.     orientationChange();  
  5.   
  6.     window.onorientationchange = orientationChange;  
  7.   
  8. });  


  • 5. 隐藏地址栏 & 处理事件的时候,防止滚动条出现:


Java代码  收藏代码
  1. // 隐藏地址栏  & 处理事件的时候 ,防止滚动条出现  
  2. addEventListener('load', function(){  
  3.   
  4.         setTimeout(function(){ window.scrollTo(01); }, 100);  
  5.   
  6. });  


  • 6. 双手指滑动事件:


Java代码  收藏代码
  1. // 双手指滑动事件  
  2. addEventListener('load',  function(){ window.onmousewheel = twoFingerScroll;},  
  3.   
  4.      false              // 兼容各浏览器,表示在冒泡阶段调用事件处理程序 (true 捕获阶段)  
  5.   
  6. );  
  7.   
  8. function twoFingerScroll(ev) {  
  9.   
  10.     var delta =ev.wheelDelta/120;              //对 delta 值进行判断(比如正负) ,而后执行相应操作  
  11.   
  12.     return true;  
  13.   
  14. };  

  • 7. 判断是否为iPhone:

Java代码  收藏代码
  1. // 判断是否为 iPhone :  
  2. function isAppleMobile() {  
  3.   
  4.     return (navigator.platform.indexOf('iPad') != -1);  
  5.   
  6. };  


  • 8. localStorage:

Java代码  收藏代码
  1.  例子 :(注意数据名称  n  要用引号引起来)  
  2. var v = localStorage.getItem('n') ? localStorage.getItem('n') : "";   // 如果名称是  n 的数据存在 ,则将其读出 ,赋予变量  v  。  
  3.   
  4. localStorage.setItem('n', v);                                           // 写入名称为 n、值为  v  的数据  
  5.   
  6. localStorage.removeItem('n');                                           // 删除名称为  n  的数据    


Java代码  收藏代码
  1. 9. 使用特殊链接:  


如果你关闭自动识别后 ,又希望某些电话号码能够链接到 iPhone 的拨号功能 ,那么可以通过这样来声明电话链接 , 

<a href="tel:12345654321">打电话给我</a> 
<a href="sms:12345654321">发短信</a> 

或用于单元格: 
<td onclick="location.href='tel:122'"> 

  • 10. 自动大写与自动修正


要关闭这两项功能,可以通过autocapitalize 与autocorrect 这两个选项: 

<input type="text" autocapitalize="off" autocorrect="off" /> 

  • 11. WebKit CSS:


①“盒模型”的具体描述性质的包围盒块内容,包括边界,填充等等。 
Java代码  收藏代码
  1. -webkit-border-bottom-left-radius: radius;  
  2.   
  3. -webkit-border-top-left-radius: horizontal_radius vertical_radius;  
  4.   
  5. -webkit-border-radius: radius;      //容器圆角  
  6.   
  7. -webkit-box-sizing: sizing_model; 边框常量值:border-box/content-box  
  8.   
  9. -webkit-box-shadow: hoff voff blur color; //容器阴影(参数分别为:水平X 方向偏移量;垂直Y 方向偏移量;高斯模糊半径值;阴影颜色值)  
  10.   
  11. -webkit-margin-bottom-collapse: collapse_behavior; 常量值:collapse/discard/separate  
  12.   
  13. -webkit-margin-start: width;  
  14.   
  15. -webkit-padding-start: width;  
  16.   
  17. -webkit-border-image: url(borderimg.gif) 25 25 25 25 round/stretch round/stretch;  
  18.   
  19. -webkit-appearance: push-button;   //内置的CSS 表现,暂时只支持push-button  


②“视觉格式化模型”描述性质,确定了位置和大小的块元素。 
Java代码  收藏代码
  1. direction: rtl  
  2.   
  3. unicode-bidi: bidi-override; 常量:bidi-override/embed/normal  


③“视觉效果”描述属性,调整的视觉效果块内容,包括溢出行为,调整行为,能见度,动画,变换,和过渡。 

Java代码  收藏代码
  1. clip: rect(10px, 5px, 10px, 5px)  
  2.   
  3. resize: auto; 常量:auto/both/horizontal/none/vertical  
  4.   
  5. visibility: visible; 常量: collapse/hidden/visible  
  6.   
  7. -webkit-transition: opacity 1s linear; 动画效果 ease/linear/ease-in/ease-out/ease-in-out  
  8.   
  9. -webkit-backface-visibility: visibler; 常量:visible(默认值)/hidden  
  10.   
  11. -webkit-box-reflect: right 1px; 镜向反转  
  12.   
  13. -webkit-box-reflect: below 4px -webkit-gradient(linear, left top, left bottom,  
  14.   
  15. from(transparent), color-stop(0.5, transparent), to(white));  
  16.   
  17. -webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)));;   //CSS 遮罩/蒙板效果  

0 0