HTML5 tricks for mobile

iOS 7.1的Safari为meta标签新增minimal-ui属性,在网页加载时隐藏地址栏与导航栏

01. Creating a fullscreen experience

On Android Browser – the default browser on Android up to 4.3, and different to Chrome – the only solution is to have a document with a height at least equal to the device‘s height, and to use the following snippet to hide the URL bar.

  1. window.addEventListener("load", function() { window. scrollTo(0, 0); });

Google Maps uses this trick to emulate a fullscreen experience on this browser. When used carefully, you can also reduce the possibility of a URL bar reappearance by preventing the browser from managing the touch scroll:

  1. document.addEventListener("touchmove", function(e) { e.preventDefault() });

In Google Chrome, Firefox OS, Firefox for Android, BlackBerry OS 10 and Amazon Silk (the browser developed for Kindle Fire) we can use the standard Fullscreen API from the W3C.

iPhone on ios, Android Browser and Chrome on Android using a fullscreen mode with different implementations. on IE11 for Windows Phone, the user needs to approve fullscreen view

As you should already know, some browsers still work with prefixes, so for this we need a multivendor code:

  1. var body = document.documentElement;
  2. if (body.requestFullscreen) {
  3.   body.requestFullscreen();
  4. } else if (body.webkitrequestFullscreen) {
  5.   body.webkitrequestFullscreen();
  6. } else if (body.mozrequestFullscreen) {
  7.   body.mozrequestFullscreen();
  8. } else if (body.msrequestFullscreen) {
  9.   body.msrequestFullscreen();
  10. }

The only important restriction to remember when requesting fullscreen mode is that we should do it only after a user‘s interaction – for example, activated by a touch operation.

Apple offers a solution for iPhone since iOS 7.1, using a special attribute of the viewport meta tag requesting a minimal UI from Safari on portrait and no UI at all on landscape. This mode is not available on iPad.

To enable the minimal UI mode, just use:

  1. <meta name="viewport" content="width=devicewidth, minimal-ui">

When this mode is enabled, you need to take some special carearound touchable areas near the edges.

To detect if the minimal UI mode is enabled:

  1. @media (device-height: 568px) and (height: 529px),
  2. (device-height: 480px) and (height: 441px) {
  3. /* minimal-ui is active */
  4. }

02. Creating a web app on the home screen

On iOS – for both iPhones and iPads – and Chrome on Android we can have a fullscreen experience after the user has installed an icon on the home screen. The web app will run outside of the browser, even from a multitasking point of view. To enable this we need to add some meta tags:

  1. <!-- for ios 7 style, multi-resolution icon of 152x152 -->
  2. <meta name="apple-mobile-web-app-capable" content="yes">
  3. <meta name="apple-mobile-web-app-status-barstyle"content="black-translucent">
  4. <link rel="apple-touch-icon" href="icon-152.png">
  5. <!-- for Chrome on Android, multi-resolution icon of 196x196 -->
  6. <meta name="mobile-web-app-capable" content="yes">
  7. <link rel="shortcut icon" sizes="196x196" href="icon-196.png">

On Firefox OS and Firefox for Android we can also create home screen web apps by creating a JSON manifest and using a JavaScript API. See the official documentation for examples.

When you define a home screen web app beside a chrome-less app, your app will have an entry in the task bar of the os, apart from the browser

03. High resolution Canvas

The Canvas API is a bitmap-based drawing interface that works as an image loaded from a file. Therefore, if you create a canvas with width=200 it will create a 200 real pixel image. On the document it will be drawn at a 200 CSS-pixel width, independent of the resolution.

That means that on an iPhone 5S, the 200 pixel image will be resized to 400 device pixels, and on a Nexus 5 device it will be resized to 600 device pixels – in both cases losing some of the resolution of your image. If you want to create a highresolution image, let‘s say double of the original, medium-resolution device, it‘s possible to use the following trick:

  1. <canvas width="400" height="400" style="width: 200px; height: 200px"></canvas>
  2. <script>
  3. document.queryselector("canvas").getContext("2d").scale(2, 2);
  4. </script>

Be aware that increasing the size of your canvas will also increase the memory used and the CPU required to create the drawings, so we should do it only when we are sure that we are dealing with a high-resolution device.

04. Truly numeric text field

I‘m pretty sure you already know all the new HTML5 types for the input element, such as type=email , that trigger different virtual keyboards on touch devices. While on Android and Windows Phone you will get a fully numeric keypad when usingtype=number, you might not realise that on iOS you don‘t get exactly that.

If you want to get a fully numeric keypad on ios, you have to add a pattern value on the input element. By default, the number type will allow the user to type letters

If you add a pattern="[0-9]*" attribute then you will get a truly numeric keypad in iOS as in other operating systems:

  1. <input type="number" pattern="[0-9]*">

You can also use this trick with type="password" to get a numeric keypad for a PIN text field.

  1. <input type="password" pattern="[0-9]*">

05. Responsive web design and Windows 8

If you are working with responsive websites, you might think that defining a mobile viewport meta tag and different breakpoints with CSS media queries is enough. However, there is one particular situation that needs a little more work: Windows 8.x with Internet Explorer that works in fullscreen mode (previously known as Metro mode).

On IE for Windows 8.x mode you can share the screen with other apps – in which case by default your website will be zoomed out, even if you have responsive breakpoints on CSS

In Windows 8 and 8.1 a website opened in this browser can share the screen with other fullscreen mode apps, including the classic desktop. In that case, and when the available width is less than 1024 pixels, IE will apply a mobile behaviour that zooms out the website and does not apply your responsive code.

To solve this issue, we can use the CSS Viewport declaration on a media query, such as:

  1. @media only screen and (max-width: 400px) {
  2.   @-ms-viewport { width: 320px; }
  3. }

06. Where is my date and time picker?

You were told that by using <input type=datetime> you get a date and time picker on most browsers. And it was true on mobile devices until iOS 7 and Chrome 26 for Android. Since those versions, that kind of form control renders as just the default text fields.

However, you may not be aware that the date and time picker is still available on those browsers using type=datetime-local, as this change was not properly announced or documented by the browser‘s vendors.

  1. <input type="datetime-local">

07. Rich editor

On HTML5 there is a new Boolean attribute for HTML elements: ContentEditable. We can use this attribute on any HTML element, including a <div>, a <table> or a <ul>.

When the user taps on those elements, the virtual keyboard will appear and the user will be able to edit it, even if it includes rich HTML – such as adding new list‘s items using the Enter Virtual key. To save updates, you should check on the innerHTMLattribute of the element.

With ContentEditable we can create a rich editor for iOs, Android, Windows Phone and other devices using the virtual keyboard on any HTML element

On iOS the user will be able to apply bold, italics and underline on a selection‘s contextual menu.

  1. <ul contenteditable>
  2.   <li>static item in the hTML
  3. </ul>

08. Safari‘s tint

Since iOS 7, Safari for iPhone and iPod touch includes a tint colour that goes behind a semi-transparent Safari UI. When your page loads, Safari looks at the background colour of your website to define that tint. There is no way to define that tint programmatically or through a meta tag, but I have a trick to enable you to chose a different colour for the tint and your background.

  1. body {
  2.   /* for safari‘s tint */
  3.  
  4.   /* for the document‘s body background color */
  5.   background-image: linear-gradient(to bottom, red 0%, red 100%);
  6. }

If your body‘s background is red, the safari UI on iPhone will tint the toolbars with the same colour. Tip 08 outlines a trick to change the colour (right)

09. Home screen icon name

When you add a website to the home screen on iOS, Chrome on Android or pin a website to a tile on Windows Phone, your HTML‘s <title> content will be used for the icon‘s name. Because of SEO techniques, we know that our title is not exactly friendly for the home screen on our mobiles.

An undocumented meta tag on Safari on iOS exists that helps us a lot with defining the name of an icon:

  1. <meta name="apple-mobile-web-app-title"content="Myname">

On Windows Phone and Windows 8.x tablets we can define the tile‘s title using:

  1. <meta name="application-name" content="Myname">

Unfortunately, there is no way to define the icon‘s name on Chrome on Android programmatically.

On Windows 8+ and Windows Phone 8.1+ the user can pin a website to the start screen. We can customise how it looks and update its contents through meta tags

10. Live tiles for Windows Phone

The user can pin websites from IE to the Windows 8.x start screen, and this feature has been added to the lastest Windows Phones. You can convert that tile on the home screen to a Live Tile, updating info and keeping the icon alive even when your website is not open anymore.

To do this we need to define some meta tags. Let‘s see first what else we need to define the tile icon:

<meta name="msapplication-TileImage" content="tile-background.png">
<meta name="msapplication-TileColor" content="#FF0000">

You can build the meta tags for your Live Tile using Build My Pinned site online tool. If you have an RSS feed it will import it and give you the code to copy and paste

For the liveness of the tile, we can define a Badge Polling URI (compatible since IE10) and/ or a Notification Polling URI (compatible since IE11). The first one can update the icon and unread elements on the tile and the latter can bring news and information that will be rendered on the Live Tile‘s animation. In both cases, we can also define the frequency of the polling operation.

  1. <meta name="msapplication-badge"
  2.  content="frequency=60;polling-uri=http://host/badge.xml">
  3. <meta name="msapplication-notification"
  4.  content="frequency=30;polling-uri=http://host/live.xml">

There is a nice Live Tile creator available on the Windows site, and a WordPress plugin available that will do the work for you.

11. Zombie tab for iOS

Browsers on mobile devices may look like their cousins on desktop but their behaviour is far from the same. Consider Safari for iPad: it has tabs. However, if you open three tabs with Facebook, Twitter and Gmail, you will only receive updates from the active tab. Every inactive tab is frozen. That means no JavaScript execution, and no way to receive updates as we are used to on desktop.

With this trick, we can take an inactive tab or window and make it come to life. Why? Well, we can receive updates from the server, and if there is something really important we can change the tab‘s title to alert the user (only for iPad), or we can just use an alert dialog to interrupt the user while they are on a different tab or window.

Some mobile browsers such as safari on the iPad look like desktop browsers, but only the active tab is alive. The Zombie Tab hack tackles this issue

The hack requires the usage of the classic refresh meta tag that triggers a page‘s reload operation. When the page is frozen, Safari will still honour that meta tag and will revive that window. But we also need to pause the reload operation while we are in the active window. To do this we shift the refreshmeta tag every x seconds (a code that won‘t fire when frozen).

  1. <meta http-equiv="refresh" content="2">
  2. <script>
  3. var mr = document.queryselector("meta"); setInterval(function() { mr.content=mr.content; }, 1000);
  4. </script>

Be careful only to use this when you really need it. I won‘t be held responsible for websites in the background annoying you while you are peacefully reading an article.

12. Handle with care

In this article, I gave you examples, hacks and undocumented features that we can use today on mobile devices. You should be aware that you are working on the edge of the known world, and the hacks that work for you now might not work properly on future browsers.

You should always remember that performance and multi-platform are key features of your web app or web content. Even when hacks are a necessary evil today, you need to ensure that your content is still compatible if the trick doesn‘t work.

http://www.creativebloq.com/html5/12-html5-tricks-mobile-81412803

时间: 2024-08-09 18:36:38

HTML5 tricks for mobile的相关文章

HTML5 PC、Mobile调用摄像头(navigator.getUserMedia)

废话少说,先贴上代码 html: 1 <div id="main" class="masthead"> 2 <div id="face_scan_camera" class="container blackbg" style="height:792px; "> 3 <div style="width:1400px;margin:0 auto;"> 4

html5+CSS3+jquery mobile构造简易WebAPP

一.搭建Android环境 每一步都要配置环境变量,自行百度即可. 步骤一:安装NodeJS 可参考http://blog.csdn.net/u010255310/article/details/52205132 步骤二:安装java的jdk 步骤三:安装Android的SDK Android Studio 下载:https://developer.android.com/studio/index.html 点击安装,官网有具体安装步骤. 步骤四:安装Apache Ant 不需要安装,只需要把路

【课程分享】HTML5开发框架PhoneGap实战(jQuery Mobile开发、API解析、3个经典项目实战)

对这个课程有兴趣的朋友可以加我的QQ2059055336和我联系 课程讲师:厉风行 课程分类:Java 涉及项目:我要地图.豆瓣音乐.小强快跑 用到技术:HTML5.jQuery Mobile.PhoneGap 其他特性:PhoneGap API 涵盖内容:代码.视频.ppt 课时数量:40 PhoneGap前景 Adobe最近公开表示将会为HTML5开发推出更多有意义的工具.有业内人士表示,Adobe的HTML5战略特别值得注意,此外Adobe对于乔布斯的此番公开批评曾积极地回应道:"乔布斯说

用jQuery Mobile做HTML5移动应用的三个优缺点

在过去大约一个月的时间里,我一直在使用JQuery Mobile为一个健身培训网站开发基于HTML5的手机/平板前端应用.我之前曾经写过Android和iOS应用程序(分别用Java和Objective-C),因此只要编写一段基础代码就可以在主流平台上运行并能够快速地用HTML和JavaScript迭代,这样的许诺十分诱人. JQuery Mobile & HTML5 使用HTML5和JavaSript构建一个手机应用,你需要写很多JavaScript代码.然而,带有触摸屏的设备的UI控制和处理

初入移动端的手忙脚乱【html5+jQuery Mobile】(一)

刚入职一家公司,面试的是Web前端开发,进来后,直接让做一个手机端app,之前是学过html5,jQuery Mobile,但只仅限于学习,制作各种小demo,一旦做起东西来,还是有些摸不着头绪的,关键的问题是,没有一个资深的带着做,另外,前端的工作还是由我一个来做,入职第一天的时候,才弄明白入职后工作这整个的一个状况,当时真是头大了. 问领导,用什么软件开发,领导说,这个你自己来定. 哇!这也太看得起了吧,心里的无限的空落呀!就这样,第一天下午,就干瞪着电脑,惆怅+茫然的下班了,要在这里干下去

7个混合式HTML5移动开发框架

在这个时间开始学习移动开发真是最好不过了,每个人应该都有一些移动应用的创意,而且你并不需要任何的原生应用编程经验,你只需要一些HTML的相关知识,懂一些CSS和JavaScript就够了.如果你总听别人说HTML5的移动应用太慢,我只能告诉你应该有一些主见,首先HTML5会越来越好,移动端的硬件也会越来越强,怎么说呢,你仔细看一看你手机上的应用吧,很多应用已经悄悄的使用混合式开发了,如果像你说的它们很慢,你发现它们了吗? 目前已经有很多的框架可以帮助你开发跨平台的移动应用,在这篇文章中,我们只介

HTML5来了,7个混合式移动开发框架

详细内容请点击 在这个时间开始学习移动开发真是最好不过了,每个人应该都有一些移动应用的创意,而且你并不需要任何的原生应用编程经验,你只需要一些HTML的相关知识,懂一些CSS和JavaScript就够了.如果你总听别人说HTML5的移动应用太慢,我只能告诉你应该有一些主见,首先HTML5会越来越好,移动端的硬件也会越来越强,怎么说呢,你仔细看一看你手机上的应用吧,很多应用已经悄悄的使用混合式开发了,如果像你说的它们很慢,你发现它们了吗? 目前已经有很多的框架可以帮助你开发跨平台的移动应用,在这篇

[HTML5_JQueryMobile]20个很棒的 jQuery Mobile 教程

构建一个餐馆选择的 Web 应用 How to build a jQuery Mobile app for choosing a restaurant based on what the user want to eat tonight, the town where they want to eat and other user’s ratings of the restaurants. 创建一个良好移动体验应用 For larger data-focused sites, or sites

Android + HTML5 混合开发

摘要: 对于 Android + HTML5 混合开发以下的观点仅仅是我的个人观点,如果有什么不对的地方请指正 简介: 混合开发的 App(Android + HTML5)就是在一个 App 中内嵌一个轻量级的浏览器(WebView),一部分原生的功能改为 HTML5 来开发,这部分功能不仅能够在不升级 App 的情况下动态更新,而且可以在 Android 或 iOS 的 App 上同时运行,让用户的体验更好又可以节省开发的资源. ##成品 APP: 超星慕课(一款包含 Java 和 C# 学习