powered by Slim Framework
enhanced by Nesbot.com

CSS3: Transitioning AJAX Content into view with CSS3 Animations

Published on Oct 17, 2011 by Jamie Munro

In a previous article, I demonstrated how you can use jQuery to Transition AJAX Content into view with $.animate(). In this article, we are going to remove the jQuery animation and use CSS3 instead.  Currently this is not supported by Internet Explorer and Opera.




Before starting, I must profess (again) that I am not a designer, so don't expect this to be pretty – just functional!  Once you have the basic functionality down, it can be prettied up quickly.

I am going to be lazy with this one and copy and paste my old article description altering as required for CSS3 because I am attempting to accomplish the exact same affect, so here we go…

The purpose of this process is to have a container with the current content a user is viewing.  Just to the right of this content should be another container (just a hint of it anyways) that the new content will be loaded into.  Once the AJAX request completes, the content will be embedded into the right container.  Once embedded, this container will be transitioned into view and the content will be swapped.

Here is an example demonstration of what I mean (I hope when I do a re-design of this site I remember to update this screenshot; otherwise, this is what the website used to look like).



This is accomplished with the following code:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Your title</title>
</head>

<body>
<div id="container">
<div id="header">
<h1>Your title</h1>
</div>

<div id="content">
<div id="active-content">
Current content goes here.  <a href="#" onclick="return slideMe()">Slide new content</a>
</div>

<div id="ajax-content"></div>
</div>
<div id="footer">
Copyright you
</div>

</div>
</body>
</html>


The basics to this HTML are that there is a container called content.  This will be the outer container.  Inside of here are two div tags.  The first, active-content, is what will house the currently viewed content.  The second div, ajax-content, should be empty – unless you wish to include some "sneak peek".

Now instead of using jQuery animations, I am going to use CSS3 animations instead.  The CSS required to perform the animations is as follows:


<style>
.ajax-content-animate {
position: absolute;
animation:mymove 1s infinite;
-moz-animation:mymove 1s infinite; /*Firefox*/
-webkit-animation:mymove 1s infinite; /*Safari and Chrome*/
}
@keyframes mymove {
from {left:900px;}
to {left:0px;}
}
@-moz-keyframes mymove {
from {left:900px;}
to {left:0px;}
}
@-webkit-keyframes mymove {
from {left:900px;}
to {left:0px;}
}
</style>


Since each browser is currently implementing the animations differently, they must be manually defined for all browsers. Hopefully this will go away in the near future because it adds a lot of extra code…

In the above example, the animation is going to start from the left at 900 pixels and move right until it reaches 0 pixels over the span of 1 second.

The final thing that needs to be done is to add the Javascript function slideMe that will perform the AJAX call and then animate the content in.


<script type="text/javascript">
function slideMe() {
$('#ajax-content').load('index2', function() {
$("#ajax-content").addClass("ajax-content-animate");
setTimeout("swapMe()", 1000);
});
}

function swapMe() {
$("#ajax-content").removeClass("ajax-content-animate");
// swap the content
$("#active-content").html($("#ajax-content").html());
$("#ajax-content").html("");
}
</script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js"></script>


When the AJAX content has finished loading, I add a class to the ajax-content div that contains the CSS animation.  I've also set a timer for 1 second so the content can be swapped into the active-content container once the animation has finished.

Here is a basic example of this process (it doesn't work in Internet Explorer or Opera).
[[##EXAMPLE##]]

Summary


Once you add some design flare to this example, you can begin making your content look like it is pre-loaded and animate it in place through AJAX and some basic CSS3 animations.

Tags: css3 | CSS

<- HTML5: Experimenting with the canvas for a basic walk animation  Home MSSQL/MySQL: Understanding how to Index your database -> 
blog comments powered by Disqus