Subscribe Via RSS

3896 Subscribers

Subscribe Via RSS
Subscribe Via Twitter

Followers

Subscribe Via Twitter
Jquery Fade In.Fade Out

January 19th, 2009 in JQuery Tutorials by Richard Carpenter

Jquery Fade In.Fade Out

1 Star2 Stars3 Stars4 Stars5 Stars166 Votes, Rating: 4.25
Loading ... Loading ...

Hi there in this tutorial il show you some simple effects you can use to spice up your websites using jquery. Your probably thinking oh no not another coding language ive got to learn.

There is an updated version of this tutorial which is alot more stable. You can view there new tutorial HERE

Dont worry its real simple to use and implement. If you really want to get stuck into jquery then nettuts have wrote a complete series on the subject SEE THIS LINK.

Firstly whats jquery? i hear you ask, well jQuery is a lightweight JavaScript library that emphasizes interaction between JavaScript and HTML.

What can i do with this code your about to give me? Basically its a fade in fade out effect. The effect fades an element to 30% on arrival of the website, then when you hover over it, it fades to 100%. The effect can be assigned to basically anything in a website wether it be an image, text, a link or even a div.

The code looks like this.

$(document).ready(function(){
$("ELEMENT HERE").fadeTo("slow", 0.3);
$("ELEMENT HERE").hover(function(){
$(this).fadeTo("slow", 1.0);
},function(){
$(this).fadeTo("slow", 0.3);
});
});

Let me explain the code above. Line 1 basically means load the javascript when the document has fully loaded, this means the javascript wont start untill the whole HTML file is loaded which includes ads also. When everything has loaded the script will run.
Line 2 means fade the element to 30%, 0.3 is 30%, at a speed of slow. Where it says “element here” that would be our ID.
Line 3 means when the mouse is hovered over “the element” fade to 1.0 at a speed of slow 1.0 equals 100% The image will then stop at 100% untill the mouse is moved off the element. Then the last bit of code means when the mouse is moved off the element fade back to 30%, 0.3 at a speed of slow.

Now im going to show you how to add this all into your website. Ive put together a small download which contains a simple example.

Extract the contents of “example_one.zip” to your desktop, inside the folder example one create a new folder called “js” Inside this folder place the jquery file which can be found here. Then create a blank notepad file and save inside the “js” folder called “custom.js” So inside the JS folder there should be 2 files. “jquery1.3.min.js” and “custom.js”. Open up the custom .js file in dreamweaver then copy and paste the jquery effect code in, then save.

You should be all set to go, just a couple of things to take note of. If you look inside the HTML code of the example you will notice inside the HEAD tages there are some lines of javascript.

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/custom.js"></script>

Its basically the same as linking a .CSS file only its javascript. Without this bit of HTML code jquery wont work. Make sure the filenames of the .JS files match yours, i do believe you need to change the 1st one from “jquery” to “jquery-1.3.min.js”.

Now for the effect, as i said before you can use the effect on almost anything inside of a HTML document, now we want to add the effect to the individual images we have in our example HTML file, if you look at the code in the HTML file regarding the images you will notice they have a class of “class=”latest_img”. The class is defined in the .CSS file. The class is a form of ID in which the images can be identified by. If we open up “custom.js” in dreamweaver then locate the words “ELEMENT HERE” inside two quotes, change “ELEMENT HERE” to the class which is “.latest_img” the effect will apply its self to everything with a class of “latest_img” in our case the images. Your .JS code should looke like this.

$(document).ready(function(){
$(".latest_img").fadeTo("slow", 0.3);
$(".latest_img").hover(function(){
$(this).fadeTo("slow", 1.0);
},function(){
$(this).fadeTo("slow", 0.3);
});
});

Save it, then view your HTML file in your browser. The effect should have been applyed to the single images. See Demo.

Pretty cool ah?, Now the effect doesnt stop there as you can apply it other things within an HTML document, how about applying it to some text? See Demo.

The text within my HTML document is in P tags within a div so if i apply the effect to the p tag within a certain div you get the effect above. You can also apply the effect to a whole div, which means everything in that div will fade even the div itself. See Demo.

Here are all the effects on one page.

Why dont you try and incorparate the fade in fade out effect in your designs, or just play around and see what you can come up with ;)

Thanks for reading

About The Author

About The Author: Richard Carpenter

Hi im Richard Carpenter and im a freelance web and graphics designer from England. I am also a regular Blogger, Tutorial Writer, and owner of Photoshop Plus. You can follow me on twitter HERE. You may also view my portfolio HERE.

 

Richard Carpenter has written 364 posts.

Be Part Of The Community!

Become part of the hv-designs community.

Subscribe Via RSS or Follow Us On Twitter.

Subscribe Via RSS Follow Us On Twitter

220 Responses to “Jquery Fade In.Fade Out”

  1. Ian Lai says:

    I must agree but simply where will i be without jQuery.
    Nice tutorial.

  2. netsnake says:

    Hmm… That’s neat! I think I should really start to code in jQuery, but 2 be honest, I’ve never really done something with JavaScript, where should I start?

    netsnake

  3. Nice tutorial!

    Click my nick

  4. Andriod24 says:

    Nice tut! Was just thinking about this yesterday morning

  5. reremoon says:

    This is a nice but very beginner tutorial… You can do a lot more with JQuery.
    Just check jquery doc with a lot of examples and plugins to learn how to use :
    http://docs.jquery.com

  6. ADMIN says:

    The tutorial is AIMED at BEGINNERS

  7. Im and idiot says:

    I didn’t get this,
    Like it doesn’t show steps by steps. It made me get very confused, I was going to use it for a school project but I really can’t. I think that you should put one up with the steps so that people know what to do instead of having to download all this stuff

  8. ADMIN says:

    download ALL what stuff??? theres an example.zip file

    the article basically tells you what you have to do

  9. shinokada says:

    If you hover quickly for many times hover effect last for some time. In order to avoid this you need to add .stop().

    Cheers

  10. Great tutorial. I’m a jQuery beginner and had a pretty easy time with it. Also, for your “Share and Enjoy” section at the bottom, did you use the same thing and where did you get those specific icons.
    Thanks and keep the tuts coming.

  11. ADMIN says:

    the share and enjoy part at the bottom of the article is a plugin for wordpress, the effect is built into that i dont no if it uses jquery.

    AS for the icons, there from dryicons.com

  12. maximus says:

    how can we make like your Share and Enjoy: widget????

  13. zelchm says:

    >shinokada 24.01.2009 at 3:33 pm
    >
    >If you hover quickly for many times hover effect last for some time. In order to avoid this you need to >add .stop().

    Could someone elaborate on how and where the .stop() is used to limit the continuation of the effect after someone has moved the cursor away

  14. PiNK says:

    How come I get a funny little box above like this?:

    http://wowimages.net/files/uysk9f83ahumaaxgnmjz.jpg

  15. zelchm says:

    I’m also having an issue with this not working with IE7. Is that a known problem? Or did I mess something up on my end. It is working fine in firefox

  16. ADMIN says:

    the effect works in both firefox and IE7,

    if you copyied & pasted the code from the tutorial double check all quotation marks and symbols are in the right place…. sometimes when you copy and paste it doesnt copy everything over.

  17. jeel says:

    thank you very match

    http://www.jeel5.co.cc/vb

  18. Damien Metcalf says:

    awesome script! really nice work!
    I have one question. Can this work on a tag?

    Thanx

  19. zelchm says:

    my bad, it was the security settings on IE that weren’t allowing it to work. Still wondering if anyone could clarify the proper use of .stop() to prevent the script from looping too many times in case someone gets crazy with the mouse. Either way, thanks for posting this.

  20. ADMIN says:

    $(document).ready(function(){
    $(“ELEMENT HERE”).fadeTo(“slow”, 0.3);
    $(“ELEMENT HERE”).hover(function(){
    $(this).fadeTo(“slow”, 1.0);
    },function(){
    $(this).fadeTo(“slow”, 0.3);
    .stop().
    });
    });

  21. Azhar says:

    thank you

  22. Chris Porter says:

    That .stop(). doesn’t work. It disables my other javascripts. I have a problem with the hover. Whenever I use the same div more than 2 times, and when I hover, it only works for two random divs, and then it doesn’t fade in on the others. It feels like the code can’t handle it when you have the javascript set for a class being used by 10 divs.

  23. Chris Porter says:

    Here is an example from what I’m working with:
    http://www.whochrisporter.com/buzzhoney/portfolio.html

    I’m using the effect after you hover over a portfolio piece, click the client name, and hover over to see the buttons pop up…do it at least three times to three different portfolio pieces

    Thanks

  24. admin says:

    $(document).ready(function(){
    $(”ELEMENT HERE”).fadeTo(”slow”, 0.3);
    $(”ELEMENT HERE”).hover(function(){
    $(this).fadeTo(”slow”, 1.0);
    },function(){
    $(this).fadeTo(”slow”, 0.3);

    $(this).stop();

    });
    });

    Adding the stop bit of code will stop the effect once it has reached 100% full opacity, it will then stay at 100% untill you hover over it again, then it will darken too 30% again.

  25. Chris Porter says:

    That wasn’t the problem. After you check out two portfolios, on the third one, the action buttons won’t fade to 100%. I don’t think its the libraries either, because I tried a regular ol’ javascript from http://www.dynamicdrive.com/, and it was doing the same thing.

  26. Thunder says:

    Hello,

    very good tutorial!!! It is possible, that I use the effect on a commercial site????

    Best regards

    Thunder

  27. Thunder says:

    Hello again,

    I have tried your example withe the Lightbox2 but it doesn’t work – the pictures open normal, not in Lightbox2. Do you know where is the problem….??? If I delete your JavaScript, Lightbox works fine.

    http://www.lokeshdhakar.com/projects/lightbox2/

  28. Daniel Kuijer says:

    Hello,

    I just tried this effect and it worked quite well for me. I have only one problem: I am using transparent 24bit PNGs and they look messed up in IE7 (black where they should be transparent). I guess most of you know about this problem. Does anyone know a solution to this? I wasn’t able to find one while researching and everything I read was pretty discouraging.

    Thanks in advance and best regards from Germany.
    Daniel Kuijer

  29. Finally, a tutorial for a simple fade in / out that works! So much time wasted reading other articles that overcomplicate the matter. Well done guys.

  30. Mecky says:

    Hmmm. I don’t see “Element Here” anywhere in the code. Am I missing something?

  31. ADMIN says:

    CHECK THE FIRST BLOCK OF CODE IN THE TUTORIAL.

    (the jquery code)

  32. Ketan says:

    This is not working for me in IE7. Works fine in Firefox. I checked the quotation marks and symbols. It all seems correct. Please help.

  33. mark says:

    It’s not working for div or element set to z-index greater then 1.. i have a DIV that has some background and overlay div.. all other part are faded except the element that has the z-index

  34. mark says:

    ok figures that only happens in IE..in FF the container DIV will fade out all contents inside it.. whereas in IE.. the nested DIV are not faded…only the ID or class associated with that individual DIV or class is faded..

  35. Julio says:

    It doesn’t work in IE7.
    Actually, I’m browsing this site with IE7 and effect doesn’t work. It does with Opera.

  36. ADMIN says:

    the effect does work in IE7

    its been tested in both IE7 and the new FF

  37. Björn says:

    Nice Work … one more reason to start with jQuery.

  38. ?? says:

    studying

  39. Student says:

    Doesn’t work for me at all…

  40. Student says:

    Ha! It works now! :]

  41. Shorty says:

    Hi, This tutorial is amazing! But I have problem with transparent PNG files in IE and CHROME :( Can you try fix it ?? Pleeease…. Another problem is, that script need to stop after fade in / fade out. you know what i mean ? Other users had same problem..

    Please, can you fix it ? Thank you very much. amazing effect.

  42. For beginners, by beginners :D

  43. Martin says:

    Hi!
    I just want to say, thank you for a great tutorial!
    I have a problem with using the fade in fade out, whit the lavalamp jquery (http://www.gmarwaha.com/blog/2007/08/23/lavalamp-for-jquery-lovers/)
    When I link the jquery for the fade in fade out the lavalamp stops working.

    Any idea how to fix this?

    Thx for the tutorial

    Martin

  44. Marco says:

    Thanks for this cool resource, but there are a bunch of things that I’ve had to fix manually, and this sounds weird to me: I don’t see why you pointed here to jquery1.3.min.js file while in the zip the html points to jquery.js instead.
    Also reading the page I didn’t get what code actually the custom.js contains.
    But taking a look at the demo I’ve seen that the custom.js is a bit different from that one showed here.
    It looks that so:

    $(document).ready(function(){
    $(“.latest_img”).fadeTo(“slow”, 0.3); // This sets the opacity of the thumbs to fade down to 30% when the page loads
    $(“.latest_img”).hover(function(){
    $(this).fadeTo(“slow”, 1.0); // This should set the opacity to 100% on hover
    },function(){
    $(this).fadeTo(“slow”, 0.3); // This should set the opacity back to 30% on mouseout
    });
    });

    $(document).ready(function(){
    $(“#text p”).fadeTo(“slow”, 0.3); // This sets the opacity of the thumbs to fade down to 30% when the page loads
    $(“#text p”).hover(function(){
    $(this).fadeTo(“slow”, 1.0); // This should set the opacity to 100% on hover
    },function(){
    $(this).fadeTo(“slow”, 0.3); // This should set the opacity back to 30% on mouseout
    });
    });

    $(document).ready(function(){
    $(“#div”).fadeTo(“slow”, 0.3); // This sets the opacity of the thumbs to fade down to 30% when the page loads
    $(“#div”).hover(function(){
    $(this).fadeTo(“slow”, 1.0); // This should set the opacity to 100% on hover
    },function(){
    $(this).fadeTo(“slow”, 0.3); // This should set the opacity back to 30% on mouseout
    });
    });

  45. Luca says:

    Hi
    I’m trying to set up a jquery site.
    I have a function similar to this article, but when I load other images via XML, the hover effect didn’t apply anymore.

    Am I wrong in something?

  46. Thanks for the tip and it was super easy to implement. I creating classes for fading images in and out to use selectively across a site. And also adjusted the fade transition to be more speedy:

    Example code
    =================

    // Fade images in
    $(document).ready(function(){
    $(“.fade_in”).fadeTo(“fast”, 0.3);
    $(“.fade_in”).hover(function(){
    $(this).fadeTo(“fast”, 1.0);
    },function(){
    $(this).fadeTo(“fast”, 0.3);
    });
    });

    // Fade images out
    $(document).ready(function(){
    $(“.fade_out”).fadeTo(“fast”, 1.0);
    $(“.fade_out”).hover(function(){
    $(this).fadeTo(“fast”, 0.3);
    },function(){
    $(this).fadeTo(“fast”, 1.0);
    });
    });

  47. Thanks a lot! :D

  48. Marius says:

    Hey Richard! Thanks for a great guide on Jquery for us beginners! Loving it! Just one thing I’m curious about, if I’ve done it right… If I were to add two div’s with text is this how it would look…?

    $(document).ready(function(){
    $(“#intro”).fadeTo(“slow”, 0.3);
    $(“#intro”).hover(function(){
    $(this).fadeTo(“slow”, 1.0);
    },function(){
    $(this).fadeTo(“slow”, 0.3);
    $(“#intro2″).fadeTo(“slow”, 0.3);
    $(“#intro2″).hover(function(){
    $(this).fadeTo(“slow”, 1.0);
    },function(){
    $(this).fadeTo(“slow”, 0.3);
    });
    });
    });

    It works for me, I’m just curious if its right since adding two underneath each other as pointed out on an earlier reply doesn’t work for me.

  49. C says:

    This didn’t work for me and I really cannot figure out why. I followed this article to a tee. I downloaded the example on zip, created a new custom.js file, pasted the code here in the custom.js file (with the .latest_img class), saved and opened my html file in a browser. The images do nothing. I tried saving the jquery file as both jquery.js and jquery.1.3.min.js. Any ideas on why this is not working?

  50. C says:

    Also, would it be possible to assign this code for a background img in css and have text overlap?

  51. Willem says:

    Hi,

    When I look at the examples and click on it, there is a blue border around the image. How can I prevent this from happening?

    Thanks!

    Willem

  52. Sarah says:

    Hey, I am having problems with this Java working with my lightbox2. The two scripts conflict and right now the lightbox works, and the fade does not. What do i have to change in the code to get it to work with Lightbox?

  53. Eyveneen says:

    jQuery is my song…..good tutorial!

  54. Roger says:

    Thanks for this tutorial. Great jQuery effect!

  55. ??? says:

    thank you

    it is very good lesson

  56. Artem says:

    Thanks for the tutorial!

    I am having an issue with the fade looping on repeated mouse-overs with this (http://www.capelladesigngroup.com/stage/smithink/build/index.html):

    $(document).ready(function(){
    $(“#caption2″).fadeTo(20, 0.0);
    $(“#sq2″).hover(function(){
    $(“#caption2″).fadeTo(“slow”, 1.0);
    },function(){
    $(“#caption2″).fadeTo(“slow”, 0.0);
    });
    });

    .stop() doesn’t do what I need and I am not sure what else to try.

    Anyone have a solution?

    Thanks for the help

  57. @ ARTEM,

    you can use the jquery coded located on this tutorial http://hv-designs.co.uk/2009/03/03/jquery-navigation/ it uses a better way to fade including the .stop() function.

    hope this helps.

    i shud really rewrite the jquery on this tutorial.

  58. Jake says:

    Great Tutorial! What i am trying to do is have one stationary image then when you hover over it fades into a different image. I am using this for a nav…. any help you have to give me some tips on doing this using you method would be great!!!

    Thanks

    Read more: http://www.webdesign.org/forum/viewtopic.php?t=15690#ixzz0LRt8wZQi

  59. bav says:

    Any chance you could do a tutorial for some sort of image gallery. So when you click onthe thumbnails you get a sliding image ect…or pop up?

  60. Hi Richard,

    Been all over your site today and I must say, I am impressed. If I understand this toot correctly, putting this in WordPress is as easy as adding the CSS and DIV’s. Assuming the js files are in the header.

    Thanks, I need to subscribe to your site. BYE.

    JIM

  61. chris owen says:

    Hi, I’m relatively new to web design and have been able to get lightbox 2 to work fine in my site. However, what I Don’t know how to do is get lightbox2 to run on a page other than index.html. Is it restricted to the index page? If not, how and where do I edit to make it run on, for example, a gallery.html page? Very grateful for any help.

  62. sakari says:

    hello
    great tutorial thanks man !!!!
    how can I apply this effect on links
    example :
    i have put “.element a ” in ” element here ” but it don’t works !
    what is the problem ?

  63. Zean Design Mexico says:

    beautiful! me servira de mucho en mis proximos proyectos..saludos

  64. Derek says:

    How do you make this fade in and fade out refresh

  65. I can not get this to work locally:

    My custom.js file:
    $(document).ready(function(){
    2 $(“.latest_img”).fadeTo(“slow”, 0.3); // This sets the opacity of the thumbs to fade down to 30% when the page loads
    3 $(“.latest_img”).hover(function(){
    4 $(this).fadeTo(“slow”, 1.0); // This should set the opacity to 100% on hover
    5 },function(){
    6 $(this).fadeTo(“slow”, 0.3); // This should set the opacity back to 30% on mouseout
    7 });
    8 });

    and then I’m using
    jquery-1.3.min.js
    or jquery-1.2.6.js
    to no avail. Any ideas?

    my image.html doc looks like:

    <link type=”text/css” rel=”stylesheet” href=”styles.css”

    etc.

    This is blowing my mind.

  66. Sam says:

    Instead of linking to and having to upload the jquery1.3.min.js can i use:

    google.load(“jquery”, “1.3.2″);

    ….which links to the latest jquery file online instead.

    I use this on some other pages on my site but i can’t seem to get it to work with this function…

    Thanks for your help.

    Sam

  67. Kel says:

    Has anyone else noticed that the alt tags don’t appear when you hover over the thumbnail using FF browser?

  68. Kel says:

    Hi Admin,

    Would you know if the alt tags should display when you mouse over the thumbnail using Firefox?

    I just want to make sure that I have followed the tutorial correctly because from what I see, it doesn’t work in FF.

    Thank you

  69. roberto says:

    Hi,
    How can i change opacity ?
    is it possible ?
    thanks

  70. Dee says:

    That’s what we call a brilliant work !

  71. nice demo for fadein and fadeout script. can i use in jquery 1.3.2 version.

  72. Nicolas says:

    i

    Can we choose time of transition in milliseconde instead of slow or fast?

    thanks

  73. Thibault says:

    Hi all

    Nice script but I’m also having a transparency issue on IE 7 and 8 with PNG files.

    Anyone figured it out???

  74. Ash says:

    Hey! Do you think there is any way to make the fading in and out constant! So that The fading In and Out will keep happening without stopping when its hovered over!

    Would Appreciate the help

    Ash

Leave a Reply