前面有一篇文章介绍了HTML5的一些新特性以及技巧, 现再来总结一些更多的针对webkit的 HTML, CSS和Javascript方面的特性.
HTML
-
让内容的宽度自适应为设备的宽度, 在做Mobile Web时必须加的一条
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
-
禁用手机号码链接(for iPhone)
<meta name="format-detection" content="telephone=no">
-
设置你网页的图标, 尺寸为57px * 57px
<link rel="apple-touch-icon" href="icon.png"/>
-
iOS 2.0+: tell iOS not to apply any glare effects to the icon
<link rel="apple-touch-icon-precomposed" href="icon.png"/>
-
iOS 4.2+ icons for different resolutions
<link rel="apple-touch-icon" sizes="72x72" href="touch-icon-ipad.png" /> <link rel="apple-touch-icon" sizes="114×114" href="touch-icon-iphone4.png" />
-
全屏启动时候的启动画面图像, 尺寸320X460 px
<link rel="apple-touch-startup-image" href="startup.png">
-
是否允许全屏显示, 只有在桌面启动时可用
<meta name="apple-mobile-web-app-capable" content="yes" />
-
控制全屏时顶部状态栏的外观, 默认白色
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
-
取消自动完成, 自动大写单词字母(适用于Mobile上)
<input autocorrect="off" autocomplete="off" autocapitalize="off">
-
语音输入
<input type="text" x-webkit-speech />
-
文件上传, 从相机捕获媒体
<input type="file" accept = "image/*; capture=camera" /> <input type="file" accept = "video/*; capture=camcorder" /> <input type="file" accept = "audio/*; capture=microphone" />
-
发送短信给多个人的链接
<a href="sms:18005555555,18005555556">
-
发送短信附带内容的链接
<a href="sms:18005555555?body=Text%20goes%20here">
-
拨打电话的链接
<a href="tel:18005555555">Call us at 1-800-555-5555</a>
CSS
-
Mobile上点击链接高亮的时候设置颜色为透明
-webkit-tap-highlight-color: transparent;
-
设置为无法选择文本
-webkit-user-select: none;
-
长按时不触发系统的菜单, 可用在图片上加这个属性禁止下载图片
-webkit-touch-callout: none;
-
全屏模式时的样式(for Desktop)
:-webkit-full-screen canvas {}
-
使用mathes来匹配多个选择器
div p :matches(em, b, strong) {}
-
指定Mobile设备或者小屏幕桌面屏幕
@media only screen and (max-width: 480px) {}
-
指定高分辨率屏幕设备
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 300dpi) { header { background-image: url(header-highres.png); } }
-
指定低分辨率屏幕设备
@media (-webkit-max-device-pixel-ratio: 1.5),(max-resolution: 299dpi) { header { background-image: url(header-lowres.png); } }
-
CSS3的背景属性
background-repeat: space; background-repeat: round;
-
计算宽度
width: calc(100%-40px);
-
波浪型链接
text-decoration: #FF8800 wavy ine-through;
-
收紧字符间的距离
text-rendering: optimizeLegibility;
-
设置CSS连字
font-variant-ligatures: common-ligatures;
-
旋转90度
transform: rotate(90);
-
transform-origin可以改变变换的位置
transform-origin: center center;
-
-webkit-appearance可以改变按钮或者其它控件看起来类似本地的控件
-webkit-appearance: none;
-
美化表单校验时的提示样式, 当提示出现时类似于下面的结构
/* html <div -webkit-validation-bubble> <div -webkit-validation-bubble-arrow></div> <div -webkit-validation-bubble-arrow-clipper></div> <div -webkit-validation-bubble-message>Error Message</div> </div> */ ::-webkit-validation-bubble {} ::-webkit-validation-bubble-message {} ::-webkit-validation-bubble-arrow {} ::-webkit-validation-bubble-arrow-clipper {}
-
自定义webkit浏览器的滚动条, 见Google Reader等在Chrome/Safari下的效果, 下面 是一个实例, 这个滚动条的样式代码如下
::-webkit-scrollbar { width: 12px; } /* Track */ ::-webkit-scrollbar-track { -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); -webkit-border-radius: 10px; border-radius: 10px; } /* Handle */ ::-webkit-scrollbar-thumb { -webkit-border-radius: 10px; border-radius: 10px; background: rgba(255,0,0,0.8); -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5); } ::-webkit-scrollbar-thumb:window-inactive { background: rgba(255,0,0,0.4); }
-
设置一个元素的背景或颜色的组合样式
-webkit-background-composite: plus-darker;
-
文字添加描边
-webkit-text-stroke: 1px rgba(0,0,0,0.5);
-
定义一个图片用来遮罩元素
-webkit-mask-image: url(/path/to/mask.png);
-
定义了一个元素的反射
-webkit-box-reflect: below 5px;
-
定义相对地址的链接样式
:local-link {font-weight: normal;}
Javascript
- 隐藏地址栏
window.scrollTo(0,0);
- 匹配媒体
window.matchMedia();
- 决定手机是否运行在WiFi/3G等网络
navigator.connection;
- 决定屏幕分辨率(iPhone 4值为2, 而Nexus One值为1.5)
window.devicePixelRatio;
- 取得网络连接状态
window.navigator.onLine
- 决定iPhone是否处于全屏状态
window.navigator.standalone;
- touch事件 (iOS, Android 2.2+)
- touchstart
- touchmove
- touchend
- touchcancel
-
gesture事件 (Apple only, iOS 2+) - give access to predefined gestures (rotation, scale, position)
- gesturestart
- gesturechange
- gesturend
-
window.orientation属性和orientationchange 事件
- window.orientation: 0 is portrait, 90 and -90 are landscape
-
devicemotion 事件
window.addEventListener("deviceorientation", function(e){ //e.alpha //e.beta //e.gamma }, false);
-
devicemotion 事件
window.addEventListener("devicemotion", function(e){ //e.accelerationIncludingGravity.x //e.accelerationIncludingGravity.y //e.accelerationIncludingGravity.z }, false);
-
requestAnimationFrame()
新的动画函数 -
element.webkitRequestFullScreen()
调用全屏函数