Professional Documents
Culture Documents
jQuery Mobile
Like jQuery UI, jQuery Mobile consists of a suite of related
components that can be used a la carte but work together seamlessly
The framework provides
an Ajax-driven navigation system (Ajax: next slide)
mobile-optimized interaction elements
advanced touch event handlers
Ajax: short for asynchronous JavaScript + XML
It's a group of interrelated Web development techniques used on the client-
side to create asynchronous Web applications
Applications can send data to and retrieve from a server asynchronously (in
the background) without interfering with the display and behavior of the
existing page
Data can be retrieved using the XMLHttpRequest object
Despite the name,
use of XML isn’t required: JSON is often used instead
the requests don’t have to be asynchronous
The following browsers support the jQuery mobile experience
Apple iOS (3.1-4.2)
Android (1.6-2.3) all devices
Blackberry 6
Windows Phone 7 Mango
Palm WebOS (1.4)
Opera Mobile (10.1)
Opera Mini (5.02)
Firefox Mobile (beta)
See http://jquerymobile.com/download/ for info on downloading and
using the CDN
Using the CDN, you have the following
<link rel="stylesheet" href=
"http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.3.min.js">
</script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js">
</script>
Continued
<div>
<ul>
<li><a href="drupal-7.html">Drupal 7 Development by Example</a></li>
<li><a href="jq-game.html">jQuery Game Development Essentials</a></li>
<li><a href="jqmobile-cookbook.html">jQuery Mobile Cookbook</a></li>
<li><a href="jquery-designers.html">jQuery for Designers</a></li>
</ul>
</div>
</div>
</body>
</html>
http://stackoverflow.com/questions/7693624/exhaustive-list-of-jquery-mobile-data-roles
Those of most interest listed first, underlined
page
header
content
footer
button
collapsible
collapsible-set
field-contain
slider
listview
navbar
We add roles to the div elements
<body>
<div data-role="page">
<div data-role="header">
<h1>Selected jQuery Books</h1>
</div>
<div data-role="content">
<ul>
<li><a href="drupal-7.html">Drupal 7 Development by Example</a></li>
<li><a href="jq-game.html">jQuery Game Development Essentials</a></li>
<li><a href="jqmobile-cookbook.html">jQuery Mobile Cookbook</a></li>
<li><a href="jquery-designers.html">jQuery for Designers</a></li>
</ul>
</div>
</div>
</body>
jQuery Mobiles notices the page
header
Renders a standard-looking
mobile header bar across the top
of the page
If text is too long for the header bar,
jQuery Mobile truncates it and adds
an ellipsis at the end
We can rotate the device and get
landscape orientation
This is all that’s required to produce Ajax navigation
To enable animated page transitions, any link pointing to an external
page is loaded via Ajax
The framework parses the link’s href to create an Ajax request and
displays the loading spinner
When the link to the page is clicked,
jQuery Mobile loads the page with an Ajax call
grabs the part of the document marked with data-role="page"
displays this content using a fading transition
Similar markup in the pages linked in the list—e.g., drupal-7.html
<div data-role="page">
<div data-role="header">
<h1>Drupal 7 Development by Example</h1>
</div>
<div data-role="content" class="book">
<img src="images/drupal-7.jpg"
alt="Drupal 7 Development by Example" />
<div class="title">Drupal 7 Development by Example</div>
<div class="author">Kurt Madel</div>
</div>
</div>
An Ajax application is often no
more than a request for a
chunk of HTML
This is Asynchronous
HTTP and HTML (AHAH)
Delivering Multiple Pages in One
Document
jQuery Mobile lets us deliver the same user experience even if all
the content is within a single document (and not delivered by Ajax)
Link the anchors within the page to internal pages with id attributes
using the # notation for fragment identifiers
Mark those divs with data-role="page" as if they were in
separate documents
If a link in a multi-page document has href="#example", jQuery
Mobile will look for a page div with id="example"
If the page is found, it will transition that page into view
You can seamlessly navigate between internal pages
(href="#example") and external pages (href="example.html")
Internal and external pages look the same to the end user except
the external pages will display the Ajax spinner while loading
Both also update the page’s URL hash to enable back button
support, deep-linking, and bookmarks
Example
<div data-role="page">
<div data-role="header">
<h1>Selected jQuery Books</h1>
</div>
<div data-role="content">
<ul>
<li><a href="#drupal-7">Drupal 7 Development by Example</a></li>
<li><a href="#jq-game">jQuery Game Development Essentials</a></li>
<li><a href="#jqmobile-cookbook">jQuery Mobile Cookbook</a></li>
<li><a href="#jquery-designers">jQuery for Designers</a></li>
</ul>
</div>
</div>
Continue
<div id="drupal-7" data-role="page">
<div data-role="header">
<h1>Drupal 7 Development by Example</h1>
</div>
Etc.
Scroll Events
scrollstart triggers when a scroll begins
scrollstop triggers when a scroll finishes
Also events that react to various stages of page navigation
And a set of virtualized mouse events that react to both mouse and
touch
Theming
See
Themes
http://demos.jquerymobile.com/1.2.0/docs/api/themes.html
ThemeRoller for jQuery Mobile
http://themeroller.jquerymobile.com/
As with jQuery UI, jQuery Mobile offers a ThemeRollerfor customizing
the look and feel of widgets
The theming system used in jQuery Mobile is similar to the ThemeRoller
system in jQuery UI with a few important improvements
It takes advantage of CSS3 properties for new effects, very
lightweight theme files, and reduced server requests
Themes include multiple color "swatches" that can be freely mixed
and matched to create visual texture
Each consists of a header bar, content body, and button states
jQuery Mobile provides 2 main swatches, "a" and "b"
To customize the look of an application, assign the data-theme
attribute a letter—e.g.,
<div data-role="page" data-theme="a">
http://api.jquerymobile.com/icons/
Adding Icons to Buttons,
http://demos.jquerymobile.com/1.2.1/docs/buttons/buttons-icons.html
jQuery Mobile provides several icons that can be used by applying a
data-icon attribute or a ui-icon- class to a suitable widget
<a href="index.html" data-role="button" data-icon="delete">
Delete
</a>
<a href="index.html" data-role="button" class="ui-icon-delete">
Delete
</a>
The value for the class prefixes ui-icon- onto the corresponding
value for the data-icon attribute
There is an SVG and PNG image of each icon
By default the SVG icons are used
Platforms that don't support SVG fall back to PNG icons
The following are some icon classes and their corresponding
images
ui-icon-delete
ui-icon-arrow-u
ui-icon-arrow-d
ui-icon-back
ui-icon-check
ui-icon-mail
ui-icon-plus
ui-icon-search
Icons and themes
Examples of the same icons sitting on top of a range of different
color swatches with themed buttons
Swatch "a" themed buttons
<label for="di">Dinner</label>
<input type="checkbox" name="meal[]" id="di" value="dinner" />
</fieldset>
</div>
<input type="submit" data-theme="b" name="submit" id="submit"
value="Submit">
</fieldset>
</form>
</div>
<label for="di">Dinner</label>
<input type="checkbox" name="meal[]" id="di" value="dinner"/>
</fieldset>
</div>
<input type="submit" data-theme="b" name="submit" id="submit"
value="Submit">
</fieldset>
</form>
</div>
</div>
</body>
</html>
Since the form element has the attribute data-ajax="false", we can
proceed with the server script somewhat as we normally do
In the PHP script that fields the submission, check that form values
have been received before we can safely work with them
The following PHP code accessing the form variables
$name = $_POST['name'];
$meal = $_POST['meal'];
Test whether either one of these is set—we’ll test $name
<?php
if (isset($name)) {
// Usual code
}
?>
All the usual PHP code is within the scope of this condition testing
whether $name is set
The entire listing of test1.php
<?php
$name = $_POST['name'];
$meal = $_POST['meal'];
?>
<html>
<head>
<title>jQuery Mobile Client-Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href=
"http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js">
</script>
</head>
Continued
<body>
<div data-role="page">
<div data-theme="a" data-role="header">
<h3>Welcome Page</h3>
</div>
<div data-role="content">
<?php
if(isset($name)){
echo " <p>Welcome, $name!</p>\n";
echo " <p>Your meals are ";
foreach ($meal as $m)
echo "$m ";
echo "</p>\n";
}
?>
</div>
</div>
</body>
</html>
Form filled out, ready to submit Rendering of the response