MOBILE

Programming the Mobile Web : WebKit CSS Extensions (part 3) - Transitions

1/20/2011 3:33:58 PM

5. Transitions

A transition is just an automatic animation that takes place when a CSS property’s value changes. The property must be defined by the browser as able to animate (typically this applies to position and size properties). There isn’t an official list of properties that animations will work on, but the general policy is that any attribute with numerical or color values should be animated using transitions. There are also a few exceptions, like the visibility discrete property.


Warning:

Remember, these transitions are defined entirely using CSS: we are not using JavaScript or any other technique to create the animations. This may sound a bit strange, but it is a simple and powerful technique.


The transitions framework is available for Safari (from iOS 2.0) and the Android browser, and transitions have enhanced performance on these devices.

To create a transition, we should:

  1. Define the transition properties (duration, delay, where to apply, timing function) in the element(s) we want to animate.

  2. Change the values of the attributes of the element(s) to animate using JavaScript, or apply classes to or remove them from the element.

  3. Verify that the animation is working.

Sounds simple, right? Let’s do it.

5.1. Animation properties

An animation can be defined using the shortcut property -webkit-transition, with the following syntax:

-webkit-transition: property duration timing_function delay [, ...];

We can also use the specific properties listed in Table 2.

Table 2. WebKit transition properties
PropertyDescription
-webkit-transition-propertyDefines which property or properties to animate. We can use a comma-separated list, or the constant value all.
-webkit-transition-durationDefines the duration of the transition. The value can be 0 (no animation) or a positive value in seconds (using s as the unit) or milliseconds (using ms as the unit). If we want to define different timings for each property, we can use a list of comma-separated values in the same order as the -webkit-transition-property value.
-webkit-transition-delayDefines the offset delay of the animation beginning from the time when the property was changed. This can be defined in seconds or milliseconds, and the default value is 0. If a negative value is used, the animation starts immediately but with some of the animation already done.
-webkit-transition-timing-functionDefines the function used to calculate intermediate values from the initial to the finish value of the property. You can use the CSS cubic-bezierease, linear, ease-in, ease-out, and ease-in-out (the most commonly used.) function, or any of the following constants:

For example, the following code produces a fade-in, fade-out animation:

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
"http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Fade Sample</title>
<style>

#box {
width: 200px;
height: 200px;
background-color: red;
-webkit-transition: opacity 2s;
}

.hide {
opacity: 0;
}


</style>
<script type="text/javascript">
function fade() {
var box = document.getElementById("box");
box.className = (box.className=="hide") ? "" : "hide";
box.innerHTML = box.className;
}

</script>

</head>
<body>

<h1>Fading</h1>
<input type="button" onclick="fade()" value="Hide-Show" />
<div id="box">
</div>
</body>
</html>


We can do similar transitions for resizing, relocation, color changes, or even 3D transitions using the transform properties that we will see in a minute.

5.2. Transition ending

The transition ending can be listened for from JavaScript just like any other DOM event, using addEventListener. You can then initiate another transition or do something else when you are sure that the animation has finished. The event to listen for is called webkitTransitionEnd.

We can listen for it using the following code:

box.addEventListener('webkitTransitionEnd', function(event) {
alert("Finished transition");
});
Other  
  •  Windows Phone 7 Development : Debugging Application Exceptions (part 2) - Debugging a Web Service Exception
  •  Windows Phone 7 Development : Debugging Application Exceptions (part 1) - Debugging Page Load Exceptions
  •  Programming the Mobile Web : JavaScript Libraries
  •  Programming the Mobile Web : Ajax Support
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 5) - Deploying the Service to Windows Azure
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 4) - Coding NotepadViewModel
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 3) - Coding the BoolToVisibilityConvert
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 2) - Coding MainPage
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 1) - Building the User Interface
  •  Building Android Apps : Detecting Browsers with WURFL
  •  Building Android Apps : Submitting Your App to the Android Market - Preparing a Release Version of Your App
  •  Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 2) - Implementing a WCF Service to Access the SQL Azure Database
  •  Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 1) - Generating an Object Model to Access the Cloud Database
  •  Windows Phone 7 Development : Using Cloud Services As Data Stores - Creating a Cloud Database
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 5) - Implementing the View Controller Logic
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 4) - Hiding the Keyboard
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 3) - Creating Styled Buttons
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 2) - Adding Text Views
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 1) - Adding Text Fields
  •  Building Android Apps : Controlling the Phone with JavaScript (part 3) - Accelerometer
  •  
    Top 10
    IIS 7.0 : Managing Web Sites - Administrative Tasks - Limiting Web Site Usage, Configuring Web Site Logging and Failed Request Tracing
    Optimizing for Vertical Search : The Opportunities in Vertical Search
    Infrastructure Security: The Host Level
    Database Availability Group Replication in Exchange Server 2010 : Comparing and Contrasting DAG Versus CCR/SCR/SCC
    Olympus M.Zuiko Digital ED 12mm f2.0 (Part 2) - Technical data, How lenses are tested
    D-Link DHP-1565 Wireless N Powerline Router
    Ipad Lion (Part 3) - Other possible features
    A Case For Quality (Part 3) - StilGut – UltraSlim Case for the new iPad, Toffee – Premium Leather Folio for iPad 2/3
    App Store Essentials (Part 1) - BUMP, BOXCAR, TUMBLR & WhatsApp Messenger
    Will Apple Be The Next Big Name in Gaming? (Part 2) - The apple iTV and the Apple TV
    Most View
    Maxell Audio Wild – Unique overhead headphone
    Java EE 6 : New features introduced in Servlet 3.0 (part 1)
    What we expect: Windows 8 Tablets
    SQL Server 2008 Command-Line Utilities : The sqldiag Command-Line Utility
    Review: Nikon D4 – The master of the dark arts (Part 3)
    Mouse Events in Silverlight
    Speed up Linux (Part 3) - Enjoy better swappiness, The four-line speed boost
    IIS 7.0 : Configuration File Hierarchy
    Integrating Exchange 2010 with SharePoint 2010
    CM Storm – Quick Fire Rapid
    Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 3) - Coding the BoolToVisibilityConvert
    Huge Screen Supertest (Part 5) - ASUS PA238Q & ASUS VG278H
    Windows 7 : Recovering After a Crash or Other Problem (part 2) - Restoring Previous Versions of Files & Recovering Files from Backup
    iPhone Programming : Table-View-Based Applications - Connecting the Controller to the Model
    Safeguarding Confidential Data in SharePoint 2010 : Enabling TDE for SharePoint Content Databases
    XNA Game Studio 4.0 : Windows Phone Sensors and Feedback (part 2) - Locating a Windows Phone with the Location Service
    Flora - Nature - Photo Expert (Part 3) - Flowers, Depth-of-field & Lighting
    WD My Book Thunderbolt Duo 4TB
    Access Control in Windows Vista
    Wireless Threats