Ever thought your buttons and lists in your mobile web app feel a bit clunky? This Zepto (and jQuery) plugin can help!
What is this?
Native mobile apps are built with the iOS/Android/WP SDK, which provides a rich set of UI components. These components have been optimised to work well on touchscreen devices.
Mobile web apps, on the other hand, suffer from a lack of a mobile SDK. As a result, UI components have not been optimised for touchscreen devices and can often feel clunky.
Tappivate is a small Zepto (or jQuery) plugin which aims to emulate the behaviour of two UI components, buttons and lists, in an attempt to make them feel more responsive.
How does it do that?
Show me some code!
OK... the following code adds tappivate's functionality to all children of
<div id="container"> <button data-tap="btn">Tap Me!</button> <ul data-tap="list"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> <ul data-tap="list nav"> <li>Nav Link 1</li> <li>Nav Link 2</li> <li>Nav Link 3</li> </ul> </div>
Check out the demo page on an iOS or Android device. Notice the active states on the buttons and list items - they follow the rules listed below.
What does this all do?
Tappivate searches for buttons and lists marked up with a
data-tap attribute. Once it finds these elements, it applies the following rules to each, based on touch events on the element.
These rules are designed to match native buttons and lists. For more information on this, check out Aanand Prasad's article on jquery.tappable.
<button data-tap="btn">Tap Me!</button>
When touched, a button will instantly add an
active class. When the touch ends, the class is retained for a short period of time (100ms) to provide the user with feedback that the button was tapped once his/her finger is removed from the screen.
There are two types of lists: regular list and navigation lists.
<ul data-tap="list"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul>
This type of list remains active even after a touchend. This is useful in the case where the entire list will be removed from the DOM after an item is tapped, e.g. if you are building a full-page list view which transitions to a full-page detail view.
A list item will not become active the instant it is touched, instead it will wait for a short period of time (150ms) to see if a touchmove (scroll) is registered. A touchmove will prevent the list item being highlighted, otherwise after a short delay the list item will be highlighted on touchstart.
A regular list item remains highlighted to indicate that the selection is chosen, even after touchend.
<ul data-tap="list nav"> <li>Nav 1</li> <li>Nav 2</li> <li>Nav 3</li> </ul>
An extension to a regular list, a nav list adds the touchend handler to deactivate a list item when it is tapped. This is useful for static navigation lists which remain in the DOM, e.g. a fixed nav bar at the top of a page.
A nav list should be targeted with a regular list
data-tap attribute too, to ensure the touchmove on scrolling is present.
tappivate uses Zepto/jQuery delegated events to ensure that all buttons/lists with the
data-tap markup nested inside the
Where is this used?
It has already been used in production, as part of the Mobile Backbone Boilerplate library. There are probably bugs, though - pull requests are welcome.
I want some more!
More details on why I wrote this plugin can be found on my blog.