手机开发必备技巧:javascript及CSS功能代码分享

1. viewport:

也就是可视区域。对于桌面浏览器,我们都很清楚viewport是什么,就是出去了所有工具栏、状态栏、滚动条等等之后用于看网页的区域, 这是真正有效的区域。由于移动设备屏幕宽度不同于传统web,因此我们需要改变viewport;

实际上我们可以操作的属性有4 个:

代码如下:
initial-scale - // 初始的缩放比例 (范围从>0 到10)

minimum-scale - // 允许用户缩放到的最小比例
maximum-scale - // 允许用户缩放到的最大比例

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;"/> // 离线应用的另一个技巧 // 隐藏状态栏 //指定的iphone中safari顶端的状态条的样式 //告诉设备忽略将页面中的数字识别为电话号码
在设置了initial-scale=1 之后,我们终于可以以1:1 的比例进行页面设计了。关于viewport,还有一个很重要的概念是:iphone 的safari 浏览器完全没有滚动条,而且不是简单的“隐藏滚动条”,是根本没有这个功能。iphone 的safari 浏览器实际上从一开始就完整显示了这个网页,然后用viewport 查看其中的一部分。当你用手指拖动时,其实拖的不是页面,而是viewport。浏览器行为的改变不止是滚动条,交互事件也跟普通桌面不一样。

2. link:

代码如下:
// 设置开始页面图片 // 在设置书签的时候可以显示好看的图标 //竖屏时使用的样式
<style media="all and (orientation:portrait)" type="text/css">

landscape { display: none; }

//横屏时使用的样式
<style media="all and (orientation:landscape)" type="text/css">

portrait { display: none; }

3. 事件 :

代码如下:
// 触摸事件
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

添加屏幕旋转事件侦听,可随时发现屏幕旋转状态(左旋、右旋还是没旋)。例子:
代码如下:
}; // 添加事件监听 addEventListener('load',function(){ orientationChange(); window.onorientationchange = orientationChange; });

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

代码如下:

6. 双手指滑动事件:

代码如下:
调用事件处理程序 (true 捕获阶段) ); function twoFingerScroll(ev) { var delta =ev.wheelDelta/120; //对 delta 值进行判断(比如正负) ,而后执行相应操作 return true; };
7. 判断是否为iPhone:
代码如下:

8. localStorage:

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

代码如下:
名称是 n 的数据存在 ,则将其读出 ,赋予变量 v 。 localStorage.setItem('n',v); // 写入名称为 n、值为 v 的数据 localStorage.removeItem('n'); // 删除名称为 n 的数据

9. 使用特殊链接

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

10. 自动大写与自动修正

关闭这两项功能,可以通过autocapitalize 与autocorrect 这两个选项:
代码如下:

11. WebKit CSS:

①“盒模型”的具体描述性质的包围盒块内容包括边界,填充等等。
代码如下:
ottom-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
②“视觉格式化模型”描述性质,确定了位置和大小的块元素。
代码如下:
bed/normal
③“视觉效果”描述属性,调整的视觉效果内容包括溢出行为,调整行为,能见度,动画,变换,和过渡。
代码如下:
④“生成内容自动编号,并列出”描述属性,允许您更改内容一个组成部分,创建自动编号的章节和标题,和操纵的风格清单的内容
代码如下:
two section three section counter-increment: section 1; counter-reset: section;
⑤“分页媒体”描述性能与外观的属性,控制印刷版本的网页,如分页符的行为。
代码如下:
⑥“颜色和背景”描述属性控制背景下的块级元素和颜色的文本内容的组成部分。
代码如下:
⑦ “字型”的具体描述性质的文字字体的选择范围内的一个因素。报告还描述属性用于下载字体定义。
代码如下:
unicode-range: U+00-FF,U+980-9FF;
⑧“文本”描述属性的特定文字样式,间距和自动滚屏。
代码如下:
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
⑨“表格”描述的布局和设计性能表的具体内容
代码如下:
otted,double,groove,hidden,inset,outset,ridge,solid
⑩“用户界面”描述属性,涉及到用户界面元素在浏览器中,如滚动文字区,滚动条,等等。报告还描述属性,范围以外的网页内容,如光标的标注样式和显示当您按住触摸触摸 目标,如在iPhone上的链接
代码如下:
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

12. 动画过渡

这是 Webkit 中最具创新力的特性:使用过渡函数定义动画。
代码如下:
属性: -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%)。简单点说,只要你有想法,你想让元素在这个过程中以什么样的方式改变都是很简单的。
代码如下:
实现模拟弹出消息框(Alert)的例子: ①定义过渡(在