- Tutorials / JavaScript / Tutorials / Mobile Content
- Monday, 17th Sep, 2012
Home » Tutorials » JavaScript » JQuery Mobile for Absolute Beginners – Part 4
Hi guys! Welcome to the part 4 of jQuery Mobile for Absolute Beginners. In this lesson,you will learn to create a simple interactive page using Touch Event provided by jQuery Mobile framework. If you haven’t already, be sure to read the previous articles of this series first!
jQuery Mobile offers several touch events that allows user to interact with jQuery page that look and feel like native mobile application. Following is the list of touch events that we could use for our jQuery Mobile project.
Ok, let’s create an interactive page using the touch events that we just learned. The idea is to have a face illustration and reveal more facial expression when swipe & tap hold on it. You can check out the demo before we proceed.
Note: Best viewed in iOS Safari & Android browser.
<!DOCTYPE html>
<html>
<head>
<title>jQuery Mobile Touch Event Demo From onlyWebPro.com</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Interactive Facial Expression</h1>
</div><!-- /header -->
<div data-role="content">
<div class="swipe_area">
<div id="pic" class="front"></div><!-- END pic -->
<p><em>Instruction:</em><br />
<strong>Swipe Left / Right</strong> to change the facial expression.<br />
<strong>Tap & Hold</strong> for a second to change back its original facial expression.</p>
</div><!-- END swipe_area -->
</div><!-- /content -->
<div data-role="footer">
<p>© Copyright 2012 by onlyWebPro.com</p>
</div><!-- /footer -->
</div><!-- /page -->
</body>
</html>
We will have a basic structure of jQuery Mobile Page where consists of header bar, content region and footer bar. Within the content region, we will have a division called “swipe_area” which contains a picture division and instruction. The picture divison contains class that gives us ability to control the facial expression using jQuery and CSS later.
<style>
#pic {background:url(swipe_pic.png) no-repeat -19px 0; width: 100px; height: 100px; margin: 0 auto;}
#pic.front { background-position: -19px 0;}
#pic.left { background-position: -127px 0;}
#pic.right { background-position: -236px 0;}
p { text-align: center; };
</style>
We have given width and height for the picture division, and assign a CSS sprites (The facial expression graphic) as background image to it.
We have re-position the background image to just show the part of the image we need when user swipe left /swipe right / tap hold on the page.
Here is the section where we make our page look lively.
<script>
$(document).ready(function() {
$('.swipe_area').bind('swipeleft', function(event) {
$('#pic').removeClass().addClass('left');
});
$('.swipe_area').bind('swiperight', function(event) {
$('#pic').removeClass().addClass('right');
});
$('.swipe_area').bind('taphold', function(event) {
$('#pic').removeClass().addClass('front');
});
});
</script>
We have bind the swipeleft, swiperight and taphold events to the swipe_area division. If user is swipe the page from right to left, a new class “left” will be added to the picture holder; Conversely, if user is swipe the page from left to right, a new class “right” will be added to the picture holder.
To change back original facial expression, user is requires to tap and hold for a second for triggering taphold event.
That’s it! Save your document and view it in your smart devices!
You have just learned about the usage of Touch Events of jQuery Mobile. You know how to make your jQuery Mobile page lively by implementing touch events such as swipe left, swipe right and tap hold.
Stay tuned for more.
PHP: Object Oriented Programming for Absolute Beginners
HTML5 Canvas For Absolute Beginners – Part 4
Introduction To Device Orientation With HTML5
CSS3: Spinner Animation
Solving CSS Float's Problem
Create Facebook App Style Slide-Out Navigation
Build Intelligent Layout Using CSS Calc() Property
Catch It!
Make A jQuery Sticky Header In 5 Minutes
Koubachi Web Game
HTML5 Brain Challenge - Maths Edition