FadeIn FadeOut html text div effect using jQuery.

jquery-logo Web 2.0 applications have became pervasive today. Lots of new features using AJAX, JavaScript, DHTML etc have been incorporated in web applications to make them rich in user appeal. One of the framework that has changed the way we write client side code is jQuery. Let us see a simple trick to FadeIn and FadeOut a text using jQuery. This code can be easily generalized for any html component. FadeIn works by changing the visibility of any text/div/html component gradually from transparent to solid and vica versa for FadeOut. For different browsers there are different ways to achieve this. Using jQuery we can avoid cross browser issues and can directly implement FadeIn/FadeOut effect.

FadeIn Example in jQuery

Consider following text in div.
<div id="sometext"> This text will Fade In and Out. </div>
Code language: HTML, XML (xml)
Now we can use jQuery to fadein and out this text. Just copy following text.
$("#something").fadeOut("slow"); ... $("#something").fadeOut(2000); ... $("#something").fadeOut("slow", function() { alert("Animation done"); });
Code language: JavaScript (javascript)
I have shown 3 tricks to fade out a div/text. In first code, a div with id something is slowly fade out using fadeOut() function. Note that we have passed an argument “slow”. You can also pass a number that represents milliseconds in this function. fadeOut() function also takes second argument which is a call back function that gets called when animation is over. Here in the example I have passed a simple handler function that alerts a text after the animation. Similarly you can use fadeIn() function to achieve fade in functionality.
$("#something").fadeIn("slow"); ... $("#something").fadeIn(2000); ... $("#something").fadeIn("slow", function() { alert("Animation done"); });
Code language: JavaScript (javascript)
The below code will fadeIn all the text in paragraph <p> tag.
$("p").fadeIn("slow",function(){ alert("Animation Done."); });
Code language: JavaScript (javascript)

Using fadeTo to reach a certain transparency.

We can use fadeTo() function of jQuery to set opacity of all matched elements to a specified opacity and firing an optional callback after completion. Only the opacity is adjusted for this animation, meaning that all of the matched elements should already have some form of height and width associated with them.
$("p").fadeTo("slow", 0.5); ... $("p").fadeTo("slow", 0.5, function(){ alert("Animation Done."); });
Code language: JavaScript (javascript)
Hope you will like this trick. jQuery is simply amazing. Update: Adding Online Demo for FadeIn FadeOut using jQuery.

Online Demo

Click Here to view Demo
Get our Articles via Email. Enter your email address.

You may also like...

30 Comments

  1. Any demo will be great to see how it works :)

  2. neel says:

    Nice tutorial , is there any demo of this example.

  3. Derrick says:

    Agreed. Can you please set up a demo? :)

  4. @Shabu, @Neel, @Derrick : Online demo added. Thanks for the comments.

  5. Nice tutorial, thanks for sharing with us…

  6. sweet tutorial! ty!

  7. Tunge2 says:

    Is it possitble to do it the other way around. if you press the button the text fade in. Instead of pressing the button and fade out

  8. Rob says:

    @Tunge2, did you ever find out, I want to do this, so if you did, could you please share how with me. Thanks

  9. Ashish says:

    Is it possible to dynamically change text using XML or Access?

  10. Ibrahim says:

    Nice. Thank you,

  11. Nice tutorial , is there any demo of this example.

  12. Nice tutorial, thanks for sharing with us…

  13. angela says:

    Thanks, nice tutorial, helped me with my jQuery if else, and fade …

  14. dekpress says:

    Is it possitble to do it the other way around. if you press the button the text fade in. Instead of pressing the button and fade out

  15. tegid says:

    Is it possible to use jquery to fade in something, say, 5 seconds, after a page had loaded?

    • Manoj says:

      yes it is possiable

  16. Nice tutorial , is there any demo of this example.

    • Vinny says:

      Taking the overeviw, this post is first class

  17. Sophia Marie Mina says:

    can u provide the whole source code plz?

  18. Tom V says:

    Is it possible to have several content items in the same div the fadein and fadeout automatically – like a rotating banner image?

  19. Sekai says:

    Is it Something or sometext in the Div id and the jQuery Id :D

  20. Please let me know where to paste the codes?
    into tha
    or the
    or ect…
    Without this info , we cannot do anything. It maybe obvious to
    pros but to me it is not.
    Please. Thanks

  21. Please let me know where to paste the codes?
    into tha head
    or the body
    or ect…
    Without this info , we cannot do anything. It maybe obvious to
    pros but to me it is not.
    Please. Thanks
    Your comment is aw

  22. wonderful post-keep ’em coming

  23. Chriss says:

    I started my project with this tutorial of your and evolved it to using the opacity fade-in and fade-out technique for images. Check out my tutorial here also on how I have done it. Thanks!
    http://www.crovean.net/single-image-opacity-fade-in-and-out-effect-using-jquery

  24. Could you provide some demo of jquery fade effect for entire the website seperated by div block. I cannot remember the name of that application.

  25. raubik says:

    Please let me know where to paste the codes?

  26. ครีมหน้าขาว says:

    Nice tutorial , is there any demo of this example.

  27. Any demo will be great to see how it works :)

Leave a Reply

Your email address will not be published. Required fields are marked *