|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
ContentsIntroductionFor the past couple of years, the Internet has been advancing rapidly. The introduction of methods such as AJAX have led the web forward, and coupled with the ever advancing developments in software, technology and users's expectations of the web, the standards of web design are being pushed ever higher. Web pages are forever becoming more user-friendly and rich, implementing sleek transitions and soft graphics; one element to these feature-rich pages is animation. Many websites feature animation through such transitions as fading or flashing buttons or roll-up and down elements. All these animations can be created fairly easy using Javascript, and this article will provide an introduction to this. PrerequisitesIn order to be able to follow this article and use it, a general understanding of Javascript is required, however as it provides only an introduction to Javascript animation, more complex concepts and methods are not explored in too much detail. Animating ObjectsThe ConceptThe concept of animating an object in Javascript is the same to that of animating anything. The object that is being animated is changed over a number of "steps" set at a certain interval, over a certain period of time. At each step the object may be changed in any number of ways, and the smooth running of this is what causes the object to appear animated. This animation concept can be implemented relatively easily using Javascript, as explained below. A Basic AnimationPersonally, my favourite animation to write in Javascript is a fade function, simply because as it only changes one property (the object's opacity), it is very easy to implement. Before getting into the code, however, there are two key functions and styles that need to be explained:
With a fade animation, the fading object's opacity is gradually increased or reduced from the starting opacity to the end opacity. This example will demonstrate a fade-out animation, from 100% opacity to 0% opacity, over 10 "steps": <script language="Javascript">
..
//Detect browser for later use
browser = undefined;
if(navigator.userAgent.indexOf("MSIE")!=-1)
browser = "IE";
else
browser = "Mozilla";
//Called to fade element
function fade(element)
{
//We will fade the object in 10 steps
var steps = 10;
//Set the starting opacity
setOpacity(element, 1);
//Loops the timer function
for(i=0; i<steps; ++i) {
setTimeout(function(){fadeCallback(element);}, (30*i));
}
}
//Callback to timer function
function fadeCallback(element)
{
//Get the current opacity
var opacity=getOpacity(element);
//Set the new opacity
setOpacity(element, opacity-0.1);
}
//Gets an element's opacity
function getOpacity(element)
{
var opacity = null;
//Get the opacity based on the current browser used
if(browser=="IE") {
filter = element.style.filter;
if(filter) {
alpha = filter.split("alpha(opacity=");
opacity = alpha[1].substr(0,(alpha[1].length-1))/100;
}
}
else {
opacity = element.style.opacity;
}
return opacity;
}
//Sets an element's opacity
function setOpacity(element, o)
{
//Set the opacity based on the current browser used
if(browser=="IE") {
element.style.filter = "alpha(opacity=" + (o*100) + ")";
}
else {
element.style.opacity = o;
}
}
..
</script>
The animation can then be run by calling <div style="width: 50px; height: 50px; background-color: red;" onClick="fade(this);">
</div>
which will fade the DIV out when it is clicked on. As this may be hard to digest I'll break each section down into more manageable chunks: ..
if(navigator.userAgent.indexOf("MSIE")!=-1)
browser = "IE";
else
browser = "Mozilla";
..
This first piece of code is merely a simple way to detect whether the browser used is IE or "Mozilla" (being a non-IE browser), as this is used later when retrieving or setting the opacity of an element. The code uses the The fade() Function..
function fade(element)
{
//We will fade the object in 10 steps
var steps = 10;
//Set the starting opacity
setOpacity(element, 1);
//Loops the timer function
for(i=0; i<steps; ++i) {
setTimeout(function(){fadeCallback(element);}, (30*i));
}
}
..
This function is called to fade an element. It takes one argument, The fadeCallback() Function..
function fadeCallback(element)
{
//Get the current opacity
var opacity=getOpacity(element);
//Set the new opacity
setOpacity(element, opacity-0.1);
}
..
This function is the callback function called by the The getOpacity() Function..
function getOpacity(element)
{
var opacity = null;
//Get the opacity based on the current browser used
if(browser=="IE") {
filter = element.style.filter;
if(filter) {
alpha = filter.split("alpha(opacity=");
opacity = alpha[1].substr(0,(alpha[1].length-1))/100;
}
}
else {
opacity = element.style.opacity;
}
return opacity;
}
..
This function gets the current opacity of the element being faded. As previously mentioned, IE and Mozilla-based browsers handle opacity differently, so this function uses the previously set The setOpacity() Function..
function setOpacity(element, o)
{
//Set the opacity based on the current browser used
if(browser=="IE") {
element.style.filter = "alpha(opacity=" + (o*100) + ")";
}
else {
element.style.opacity = o;
}
}
..
This function sets the opacity of the element being faded. Like the As previously mentioned, as this is only an introductory article, I have only covered how to fade out elements, however, the principles for fading in elements are the same, except that the starting opacity is 0 and the opacity is increased at each step. setTimeout() or setInterval()?What is the difference?There are two key functions in Javascript that can be used to animate objects. The first of these, which has already been demonstrated, is So which function is the best to use? After experimenting with both methods, I have found them to be both equally as useful, and both require code to be written to check whether the animation has finished or not; for An Example Using setInterval()An example of an animation using the ..
//Detect browser for later use
..
//Stores the setInterval() ID
timer = null;
//Called to fade element
function fade(element)
{
//Set the timer interval to 30ms and store the setInterval() ID
timer = setInterval(function(){fadeCallback(element);}, 30);
}
//Callback to timer function
function fadeCallback(element)
{
//Get the current opacity
var opacity=getOpacity(element);
//Clears the interval if at the end of the animation
if((opacity-0.1)==0)
clearInterval(timer);
//Set the new opacity
setOpacity(element, opacity-0.1);
}
//Gets an element's opacity
function getOpacity(element)
{
..
}
//Sets an element's opacity
function setOpacity(element, o)
{
..
}
..
This example is very similar to the example that uses the if((opacity-0.1)==0)
clearInterval(timer);
This utilises the timer = setInterval(function(){fadeCallback(element);}, 30);
As you can see, the ConclusionAfter reading this article, you should be able to:
History23rd July 2008: Unmodified first copy
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||