欢迎您加入789分类目录! (我要快速审核) (我要上推荐位
当前位置:789分类目录 » 站长资讯 » 站长运营 » 文章详细 订阅RssFeed

移动端web开发技巧与经验分享

来源:本站原创 浏览:175次 时间:2015-12-02

移动端web开发技巧汇总与经验分享,已经涵盖了web移动端开发的方方面面,web移动端开发不可多得的干货,值得一看。

meta标签,这些meta标签在开发webapp时起到非常重要的作用

(1)<meta content=”width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0″ name=”viewport” />

(2)<meta content=”yes” name=”apple-mobile-web-app-capable” />

(3)<meta content=”black” name=”apple-mobile-web-app-status-bar-style” />

(4)<meta content=”telephone=no” name=”format-detection” />

 淘宝分类目录整理发布

第一个meta标签表示:强制让文档的宽度与设备的宽度保持1:1,并且文档最大的宽度比例是1.0,且不允许用户点击屏幕放大浏览;尤其要注意的是content里多个属性的设置一定要用分号+空格来隔开,如果不规范将不会起作用。

其中:

width – viewport的宽度

height – viewport的高度

initial-scale – 初始的缩放比例

minimum-scale – 允许用户缩放到的最小比例

maximum-scale – 允许用户缩放到的最大比例

user-scalable – 用户是否可以手动缩放

第二个meta标签是iphone设备中的safari私有meta标签,它表示:允许全屏模式浏览;

第三个meta标签也是iphone的私有标签,它指定的iphone中safari顶端的状态条的样式;

第四个meta标签表示:告诉设备忽略将页面中的数字识别为电话号码

 

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

点击与click事件

对于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://johanbrook.com/browsers/native-momentum-scrolling-ios-5/ 页面描述 

<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的最上面的时间、信号栏是黑色的,当然也可以设置其它参数,详细参数说明在:http://developer.apple.com/library/safari/#documentation/appleapplications/reference/SafariHTMLRef/Articles/MetaTags.html

<meta name="apple-touch-fullscreen" content="yes" />

<meta name="apple-mobile-web-app-capable" content="yes" /> 

这2个描述也很有用的,如果没有它,你的web app会用safari浏览器打开,有了它,就会用独立进程的无地址栏的打开,完全可以和普通的APP比拟了

下面还有个不错的css,是用来区分视网膜屏幕的,这样你可以在iphone这样的手机里载入2x的图片,就不会模糊了

@media only screen and (-webkit-min-device-pixel-ratio:1.5),only screen and (min--moz-device-pixel-ratio:1.5),only screen and (min-device-pixel-ratio:1.5),only screen and (min-resolution:200dpi)

{

#logo{background-image: url(logo@2x.png);}

}

样式表:

<link rel=”apple-touch-startup-image” href=”startup.png” /> // 设置开始页面图片

<link rel=”apple-touch-icon” href=”iphon_tetris_icon.png”/> // 在设置书签的时候可以显示好看的图标

<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> 

 事件 : (请参考:指尖的下JS 系列文章http://www.cnblogs.com/pifoo/archive/2011/05/23/webkit-touch-event-1.html)

// 手势事件

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 

 屏幕旋转事件:onorientationchange

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

// 判断屏幕是否旋转

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;

    };<br>};

// 添加事件监听

addEventListener('load', function(){

    orientationChange();

    window.onorientationchange = orientationChange;

});

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

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

addEventListener('load', function(){

        setTimeout(function(){ window.scrollTo(0, 1); }, 100);

});

 双手指滑动事件:

// 双手指滑动事件

addEventListener('load',  function(){ window.onmousewheel = twoFingerScroll;},

     false              // 兼容各浏览器,表示在冒泡阶段调用事件处理程序 (true 捕获阶段)

);

function twoFingerScroll(ev) {

    var delta =ev.wheelDelta/120;              //对 delta 值进行判断(比如正负) ,而后执行相应操作

    return true;

};
 

判断是否为iPhone:

// 判断是否为 iPhone :

function isAppleMobile() {

    return (navigator.platform.indexOf('iPad') != -1);

};

localStorage:

例子 :(注意数据名称  n  要用引号引起来)

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" />

 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(* , *)。

 

 

实现模拟弹出消息框(Alert)的例子:
 

转载请注明-原文链接:移动端web开发技巧与经验分享

推荐站点

  • 123目录网123目录网

    中文网站分类目录, 免费收录各种优秀网站, 全人工审核编辑, 为百度、谷歌、有道、搜狗、必应等搜索引擎提供索引参考, 同时也能为站长们推广网站增加排名

    123.klxjz.cn
  • 网站之家网站之家

    免费收录各行业优秀网站,让你的网站快速得到百度、搜狗、360等搜索引擎的收录,同时增加优质外链,提升网站排名。

    wz.klxjz.cn
  • 五洲会海购五洲会海购

    五洲会,官方认证,正品保证,进口商品海外购物商城。轻松购遍海外进口母婴,进口保健,进口美妆等,100%原装进口,海外直采,保税发货,国内售后,网购海外商品有保障。海淘购物首选,五洲会

    www.wzhouhui.com
  • 药方大全网药方大全网

    本站为您搜集整理了大量的验方偏方秘方古方等中药方(中药方剂)八万多个。你是你自己的中医,我是你的中药方!中医中药秘方网,偏方大全愿为您的健康保驾护航。

    www.zhongyf.com
  • 窝读谷中文网窝读谷中文网

    窝读谷中文网是质量最好的中文小说网站,提供奇幻玄幻,武侠仙侠,历史军事,游戏竞技,都市青春言情,科幻灵异悬疑等中文免费小说全文阅读与小说txt下载。

    www.wodugu.com
  • 西瓜电影西瓜电影

    一一影视网(www.yjytv.com)第一时间为你提供西瓜影音2015最新电影大全,最新好看的2015好看的电影电视剧排行榜西瓜影音电影免费在线观看,更新最快的西瓜影音电影电视剧网站!

    www.yjytv.com