I always get bored with the same stuffs. And, I see same kind of content navigation to the different websites. Click a link, let’s the whole web page gets loaded and new page gets displayed. It would be better to show another content in the same page with few animation without loading the whole page.
In this post, I’ll show you how to make various animation to the content using jQuery. First of let’s start looking at the HTML and CSS code,
HTML Code for animated content navigation
<a href="#home" class="linkclass" >Home</a> <a href="#about_us" class="linkclass" >About Us</a> <a href="#service" class="linkclass" >Services</a> <a href="#contact_us" class="linkclass" >Contact Us</a> <div class="container"> <div id="home" class="msg_body"> content goes here </div> <div id="about_us" class="msg_body"> content goes here </div> <div id="service" class="msg_body"> content goes here </div> <div id="contact_us" class="msg_body"> content goes here </div> </div>
As you can see above, the “href” attribute of the anchor tag is pointing to the id of the “div”. “Home” anchor tag is pointing to “#home”. In jquery, id of a element is represented by adding “#” prefix to the id of that element.
CSS code for animated content navigation
.container { width:312px; margin-top:20px; } .msg_body { border:1px solid #CCCCCC; padding: 5px; width: 300px; background-color:#F4F4F8; text-align:justify; position:absolute; } .linkclass { font-weight:bold; color:#006699; }
Above CSS code is pretty straight forward and you can change it according to your need. Note that, there is position attribute to “absolute” in the “msg_body” class. Now let’s look at various the javaScript code in jQuery to make the various animated content navigation
Faded content navigation
//hide the all div except first one
$('.msg_body:not(:first)').hide(); //when the anchor is clicked content gets faded $("a.linkclass").click(function() { $('.msg_body').fadeOut("slow"); $($(this).attr("href")).fadeIn("slow"); });
In the first line of javaScript code, all the element with class “msg_body” is made hidden except the first element with same class. And when the anchor with class “linkclass” is clicked, the element with class “msg_body” gets faded out which is being displayed in browser. And, the element with the id value in the “href” attribute of the clicked anchor tag, get displayed in browser with fading effect.
Shutter like content navigation effect
//hide the all div except first one $('.msg_body:not(:first)').hide(); //when the anchor is clicked content gets faded $("a.linkclass").click(function() { $('.msg_body').hide("fast"); $($(this).attr("href")).show("slow"); });
The code is same as the above code but here “hide()” and “show()” function of jQuery is used to hide and display the content in different manner.
Sliding Content Navigation Effect
//hide the all div except first one $('.msg_body:not(:first)').hide(); //when the anchor is clicked content gets faded $("a.linkclass").click(function() { $('.msg_body').slideUp("slow"); $($(this).attr("href")).slideDown("slow");); });
This is another content navigation effect with sliding manner. “slideUp()” and “slideDown()” functions of jQuery is used to display this kind of effect.
Leave a Reply