Selasa, 13 Mei 2014

jQuery : Fading elements

Doing simple animation is very easy with jQuery. One of the effects it supports out-of-the-box, is fading an element in and out of visibility. Here's a simple example, where we fade in an otherwise hidden box, using the fadeIn() method:

<div id="divTestArea1" style="padding: 50px; background-color: #89BC38; text-align: center; display: none;">        <b>Hello, world!</b></div><a href="javascript:void(0);" onclick="ShowBox();">Show box</a><script type="text/javascript">function ShowBox(){        $("#divTestArea1").fadeIn();}</script>
You can fade a lot of different elements, like divs, spans or links. The fadeIn() method can take up to three parameters. The first one allows you to specify the duration of the effect in milliseconds, or "fast" or "slow", which is the same as specifying either 200 or 600 milliseconds. Here's an example of it in use:

<div id="divTestArea21" style="width: 50px; height: 50px; display: none; background-color: #89BC38;"></div><div id="divTestArea22" style="width: 50px; height: 50px; display: none; background-color: #C3D1DF;"></div><div id="divTestArea23" style="width: 50px; height: 50px; display: none; background-color: #9966FF;"></div><a href="javascript:void(0);" onclick="ShowBoxes();">Show boxes</a><script type="text/javascript">function ShowBoxes(){        $("#divTestArea21").fadeIn("fast");        $("#divTestArea22").fadeIn("slow");        $("#divTestArea23").fadeIn(2000);}</script>
Don't mind all the HTML, it's just there so that you can see the difference between the fading durations. Now, the second parameter can either be the name of an easing function (which we won't use in this tutorial) or a callback function that you may supply, to be called once the effect is done. Here's an example of that, combined with the use of the fadeOut() method, which obviously has the reverse effect of fadeIn():

<div id="divTestArea3" style="width: 50px; height: 50px; display: none; background-color: #89BC38;"></div><script type="text/javascript">
$
(function(){        $("#divTestArea3").fadeIn(2000, function()        {                $("#divTestArea3").fadeOut(3000);        });});</script>
There may be situations where you want to fade an element in our out depending on its current state. You could of course check if it is visible or not and then call either fadeIn() or fadeOut(), but the nice jQuery developers have supplied us with a specific method for toggling an element, called fadeToggle(). It takes the same parameters as fadeIn() and fadeOut(), so it's very easy to use. Here's a little example:

<div id="divTestArea4" style="width: 50px; height: 50px; display: none; background-color: #89BC38;"></div><br /><a href="javascript:void(0);" onclick="ToggleBox();">Toggle box</a><script type="text/javascript">function ToggleBox(){        $("#divTestArea4").fadeToggle("slow");  }</script>
And that's how easy it is to use the fading effects of jQuery.













Source:http://www.tutorialsdesk.com/2012/08/jquery-fading-elements.html

Tidak ada komentar:

Posting Komentar