Onextrapixel
|
Adding the final touches to a site can be the difference between a polished and beautiful site that looks “refined,” and a mediocre site that leaves no impression on visitors. jQuery, the versatile JavaScript library, can be leveraged to create all these fine tuned elements. Today we’re going to look at how to use it to create elegant page transitions. Let’s get to it!
Getting Started With HTML/CSSTo begin, let’s dive straight into the CSS. We’ll need to change the CSS for the and set it to
display: none.body { display: none; }
This will stop all of the visual elements in the Therefore, a better solution will be adding the <script type="text/javascript"> $(document).ready(function() { $("body").css("display", "none"); }); </script>
Your HTML doesn’t have to change much to get these transitions working, but before we get started on that, let’s download and attach jQuery to our page. Visit jQuery and download the latest version of jQuery – it’s easiest to just right-click the link and select “Save Linked File As”. I would suggest downloading the minified version since we’re not going to be changing the source code, but just building on the library. After downloading jQuery , move the file to a favourable location of your choice in your site directory. Now let’s see the HTML needed to attach jQuery to our site. <script type="text/javascript" src="js/jquery-1.4.1.min.js"></script> I suggest appending this in the Coding Page jQuery TransitionsOnce that’s complete, we need to write the jQuery code that creates the fade transition. First, let’s deal with page <script type="text/javascript"> $(document).ready(function() { $("body").css("display", "none"); $("body").fadeIn(2000); }); </script>
The first line of the jQuery script assures that the document is ready before it applies the CSS and fades in. The second line applies the CSS style to hide the Now, if we fire up our browser we can see the new transition in action! But if we click on any of the links, our site disappears like normal. Wouldn't it be nice to have a subtle fade effect when visitors leave one page to go to another? Let's hook that function up. First, let’s apply a special class on the links we wish to create a fade out effect. <a href="otherPage.html" class="transition">LINK</a> Now, if we go back to our HTML, we can modify our script to get some great looking jQuery fades. <script type="text/javascript"> $(document).ready(function() { $("body").css("display", "none");
$("body").fadeIn(2000);
$("a.transition").click(function(event){ event.preventDefault(); linkLocation = this.href; $("body").fadeOut(1000, redirectPage); });
function redirectPage() { window.location = linkLocation; } }); </script>
Now, we’ve asked our jQuery script to listen for visitors that click on anchors with a class of transition. If a visitor does click on it, the script immediately cancels the browser redirect, then saves the destination URL in a variable called |
Related Posts
- Form Input Placeholder Text
- Style Your Site According to the Weather with jQuery
- Easy Sequential Animations in jQuery
- How to Create a Simple iTunes-like Slider
- 20 Cool jQuery Tricks for Web Developers



Follow Us!