惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - harry.guo

转:Codeigniter调用PHPExcel例子 转:iPhone Android Web开发(概要) 转:jquery弹出层背景变暗 - harry.guo - 博客园 转:c#播放音频文件 - harry.guo - 博客园 转载:Android模拟器的基本操作 安装android sdk 遇到几个问题 基础:JDK的概念、组成及JDK常用包(转) 基础:JDK与JRE(转) 基础:C#装箱拆箱的计算 转:对比MFC,Winform,WPF 正则在FireFox和IE下使用test的不同 - harry.guo - 博客园 格式化日期输出 - harry.guo - 博客园 知识:软件复杂度 转:如何让你的SQL运行得更快 白盒测试步骤 - harry.guo Ms时间处理收集 - harry.guo [摘]javascript的Prototype实现和OO开发- - - harry.guo java实现对文件的各种操作(转) - harry.guo 实现table跳转到指定行,并改变所在行的样式! - harry.guo
转:iphone/ipad网站开发技巧整理
harry.guo · 2010-11-16 · via 博客园 - harry.guo

 原文:http://uecss.com/iphone-ipad-web-development-skills.html

iphone/ipad异常刚猛,在此把收集到的一些开发技巧罗列出来,方便项目中参考使用,如下:

侦测iPhone/iPod

开发特定设备的移动网站,首先要做的就是设备侦测了。下面是使用Javascript侦测iPhone/iPod的UA,然后转向到专属的URL。

1if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
2if (document.cookie.indexOf("iphone_redirect=false") == -1) {

虽然Javascript是可以在水果设备上运行的,但是用户还是可以禁用。它也会造成客户端刷新和额外的数据传输,所以下面是服务器端侦测和转向:

1if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod')) {

设置viewpoint和屏幕等宽

如果不设置viewpoint,网站在viewpoint就会显示成缩略形式。如果你专门为iPhone/iPod开发网站,这一条很有用,而且很简单,只需要插入到head里就可以:

1<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">

使用iPhone规格图标

如果你的用户将你的网站添加到home screen,iPhone会使用网站的缩略图作为图标。然而你可以提供一个自己设计的图标,这样当然更好。图片是57×57大小,png格式。不需要自己做圆角和反光,系统会自动完成这些工作。然后将下面这条加入head中:

1<rel="apple-touch-icon" href="images/youricon.png"/>

阻止旋转屏幕时自动调整字体大小

-webkit-text-size-adjust是webkit的私有css:

1html, body, form, fieldset, p, div, h1, h2, h3, h4, h5, h6 {-webkit-text-size-adjust:none;}

侦测设备旋转方向

iPhone可以在横屏状态下浏览网页,有时候你会想知道用户设备的手持状态来增强可用性和功能。下面一段Javascript可以判断出设备向哪个方向旋转,并且替换css:

01window.onload = function initialLoad() {updateOrientation();}
03function updateOrientation(){
04var contentType = “show_”;
05switch(window.orientation){
07contentType += “normal”;
11contentType += “right”;
19contentType += “flipped”;
22document.getElementById(“page_wrapper”).setAttribute(“class”, contentType);

iPhone才识别的CSS

如果不想设备侦测,可以用CSS媒体查询来专为iPhone/iPod定义样式。

1@media screen and (max-device-width480px) {}

缩小图片

网站的大图通常宽度都超过480像素,如果用前面的代码限制了缩放,这些图片在iPhone版显示显然会超过屏幕。好在iPhone机能还够,我们可以用CSS让iPhone自动将大图片缩小显示。

1@media screen and (max-device-width480px){
2img{max-width:100%;height:auto;}

注意如果原图片非常大,或一个页面非常多图,最好还是在服务器端缩放到480像素宽,iPhone只需要在正常浏览时缩略到320像素。这样不会消耗太多流量和机能。

默认隐藏工具栏

iPhone的浏览器工具栏会在页面最顶端,卷动网页后才隐藏。这样在加载网页完成后显得很浪费空间,特别是横向屏幕时。我们可以让它自动卷动上去。

1window.addEventListener('load'function() {
2setTimeout(scrollTo, 0, 0, 1);

使用特殊链接

这两条不用说了吧:

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

模拟:hover伪类

因为iPhone并没有鼠标指针,所以没有hover事件。那么CSS :hover伪类就没用了。但是iPhone有Touch事件,onTouchStart 类似 onMouseOver,onTouchEnd 类似 onMouseOut。所以我们可以用它来模拟hover。使用Javascript:

1var myLinks = document.getElementsByTagName('a');
2for(var i = 0; i < myLinks.length; i++){
3myLinks[i].addEventListener(’touchstart’, function(){this.className = “hover”;}, false);
4myLinks[i].addEventListener(’touchend’, function(){this.className = “”;},false);

然后用CSS增加hover效果:

1a:hover, a.hover { /* 你的hover效果 */ }

这样设计一个链接,感觉可以更像按钮。并且,这个模拟可以用在任何元素上。

iphone fixed positioning

关于漂浮定位,测试后发现 { position: fixed; } 不能为其用,
可以改为 { position:absolute; } 来实现,可以使用iphone看下DEMO:iphone-fixed-positioning

参考资料:

iPhone CSS—tips for building iPhone websites

iPhone & iPod Detection Using Javascript

iPhone Development: 12 Tips To Get You Started

Tutorial: Building a website for the iPhone

hover pseudoclass for the iPhone

fixed-positioning-on-mobile-safari

Preparing Your Web Content for iPad