Subscribe Via RSS

3896 Subscribers

Subscribe Via RSS
Subscribe Via Twitter

Followers

Subscribe Via Twitter
Jquery Fade In, Fade Out Effect – Updated

January 13th, 2010 in JQuery Tutorials by Richard Carpenter

Jquery Fade In, Fade Out Effect – Updated

1 Star2 Stars3 Stars4 Stars5 Stars42 Votes, Rating: 4.48
Loading ... Loading ...

Hello welcome to another tutorial by hv-designs, today we’ll be re-visiting the “jQuery Fade In, Fade Out Effect” that i posted up earlier last year. There was no point updating the old post as it is over a year old, so here’s the new one.

Dont Know jQuery?

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.

Whats jQuery?

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 50% 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.

New Revised Code

$(function() {
// OPACITY OF BUTTON SET TO 50%
$("ELEMENT HERE").css("opacity","0.5");

// ON MOUSE OVER
$("ELEMENT HERE").hover(function () {

// SET OPACITY TO 100%
$(this).stop().animate({
opacity: 1.0
}, "slow");
},

// ON MOUSE OUT
function () {

// SET OPACITY BACK TO 50%
$(this).stop().animate({
opacity: 0.5
}, "slow");
});
});

The code is pretty much the same as the last snippet only this time is contains some “stop” functions which stops the transition from repeating over and over again. This seemed to be major problem with the last tutorial.

Adding The Effect Into Your Website

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”. Then inside the “js” folder place the jquery libary file which can be found here.

Create a blank notepad file then go to “file > save as”, save the blank notepad file inside the “js” folder called “custom.js”. 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 into the document, 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 tags 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”.

The Effect

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 look like this.

$(function() {
// OPACITY OF BUTTON SET TO 50%
$(".latest_img").css("opacity","0.5");

// ON MOUSE OVER
$(".latest_img").hover(function () {

// SET OPACITY TO 100%
$(this).stop().animate({
opacity: 1.0
}, "slow");
},

// ON MOUSE OUT
function () {

// SET OPACITY BACK TO 50%
$(this).stop().animate({
opacity: 0.5
}, "slow");
});
});

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

More Demo’s

Pretty cool ah?, Now the effect doesnt stop there as you can apply it to 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

81 Responses to “Jquery Fade In, Fade Out Effect – Updated”

  1. Steve says:

    Nice Richard

    I used the Nettuts series quite a while back, and found it very useful as my skills progressed with jQuery.
    Jeffrey has a really nice way of putting screencasts together.

    You should do more of them yourself, I find screencasts of 5 minutes or less are actually quite addictive to watch, I find my self going from one to another when I find a site with some good short casts.
    This would have made a nice screencast. :)

    Thanks again for a nice article.

  2. Steve says:

    BTW – Your comments box does not scroll, at least not in FF 3.6 RC.
    My last post got cut off at the smilie, and I couldnt see the last line I was typing.
    Phew no typos! :P

  3. DxDesigns says:

    Awesome tutorial, something that is quite useful and effective!

  4. unknown says:

    nice tutorial and i really didnt even know what Jquery is.

    But please i have a little question; i wanna know the solution for unaligned text when you type alotta letters stick together in your comment area.

    you, too, have the same problem right here.
    thanx.

  5. Jens says:

    Why doesn’t it work here ?? I do everything step by step as described…

  6. Steve says:

    When you say you have done everything step by step as described, have you assigned your own CSS selectors in the jquery code?
    If you type it out as it has been written above, but you have your own CSS id and classes then it wont work unless you specify your selectors to work with.

  7. Ashish says:

    hi,

    Thank you very much for this really very useful article. This is very easy to use and understand jQuery for those people (like me) also who just scare to edit a jquey file if needed also.

    Thank you again for this nice article.

  8. SkullTraill says:

    Awesome one babe!

  9. DRX-Design says:

    Amazing!!
    It’s awesome
    Thank you

  10. Swenn says:

    Thank you for this tutorial! I would like to ask you to make a tutorial on how to create a simple image gallery (possibly jQuery related?) I’ve been wanting to create one for a while but I simply lack the ability to code something like that.

    Thank you!

  11. ndowd says:

    I am using a png for the image which has a photo with some text outside of the photo. In IE only, the text loses a lot of quality yet the photo looks fine. Have you come across this issue before.

    Also, if I display the image in IE without the fade code it display properly.

    Otherwise great tool!

    Please Help!

  12. thank for code

  13. Ned says:

    Any idea how this is accomplished?

    http://www.draftfcb.com/work.aspx?

    When hovered over a thumb, the others fade, leaving the hovered thumb at 100%.

  14. CBSLater says:

    Awesome tutorial! Thanks for posting this. Do you know of any way of incorporating a forward and back button to slide through the pictures? Thanks for any help!

  15. Gerard says:

    First, I do everything as you described, but it doesn’t work. After going through every step, I found I changed the the sequence of the scripts as below


    ,which I think you should emphasize in the tutorial, thanks your time :)

  16. Paul says:

    Great, thanks!!!

  17. Fez says:

    Yeah I saw the demo of your other one, I was gonna ask you to use .stop().animate but you already did that here :D

  18. @Ned did you manage to work out how you fade others out? Ive been working at it but not going anywhere

  19. @NED, i like that effect, il do some work and see if i can accomlish the same effect. WATCH THIS SPACE

  20. Emil says:

    Hi there!
    Great JQuery script but when i shows it in IE the animations gets crappy, an i dont know why=(

    Here is my websit (obs it’s just BETA so far)
    http://spotifree.org/index1.php

  21. @Ned, I have worked out how to do it… I will just write it up and make sure its actually working right and I will post it…

    @Richard Carpenter Hi, great site, I would love to see how you create the effect. Quite possibly it will be better then mine :P

  22. Ce. says:

    @Kevin + @Richard…

    Did you guys make any headway with the fade out all like the link Ned posted? I haven’t looked at the code over at the other site yet, but wanted to find out the progress. I’ll take a gander and if I come up w/ anything I’ll post back.

  23. Hi, great post thanks for sharing. JQuery can do so much great stuff.

  24. Dade says:

    how would you get a div to scroll up from the bottom say it was 300px high it would scroll and show the div 300px from the bottom then would slide down after say 5 seconds?

  25. Maarten says:

    Hi,

    I implemented your script into my website but occasionally the background doesn’t cover the whole screen, it stops just below the lowest div. Any thoughts about this?

    Thanks alot!

    Greetz!

  26. grrrrr says:

    Awsome! This is great! Thanks a lot!

  27. Ned says:

    @kevin lofthouse – Great! Looking forward to seeing the solution you came up with.

  28. Caroline says:

    Hi Richard,

    thanks for the tutorial, I’m now using this for a really simple but sophisticated (hopefully) navigation bar, cheers

  29. @Ned sorry its been a while im sure i posted the link… clearly not.

    http://www.iamkreative.co.uk/blog/design/naf-jquery/

    tutorial to the effect.

  30. clack says:

    Hello Richard,

    great effect, but it does not work in IE 8 on 24bit PNGs with alpha channels – transparency is simply displayed as black. Works in Opera 10.51, Firefox 3.6.2 and Chrome 4.1.249.1042, though. I believed that these PNG transparency problems had been fixed in IE in the meantime…

    Regards, Thom@s

  31. Jason Day says:

    Hi,

    great simple plugin – I’m using it on http://jasondaydesign.com.
    The site uses css & jquery to animate columns. What I would like to do is set the active column to not fade out when the mouse moves away, but only fade back out when a new column is selected as the active column. Each column has a class of info-col and the active column class is added when active, cur-col.

    Thoughts?

    Thanks!

  32. Jason Day says:

    OK, another question.

    Is it possible to link elements? Let me explain. I will have a lot of divs laid out with jquery masonry. I would like to have a “menu” such as -all-print-web-logo- , so when a user selects “print” all items that have the class print will be at 100% opacity while the rest are faded out.

    Possible?

    Thanks

  33. Uzair says:

    First, i would like to congratulate, you have a great blog, very knowledgeable.

    Regarding the effect, once again wonderful update. Although i would like to ask how can i link the whole ‘div’ in the jQuery code you have provided.
    If the div i want to give this effect is called ‘#logotype’, how can i replace it with “ELEMENT HERE” ?

    Regards
    Uzair.

  34. @UZAUR:

    then you simply replace “element here” with #logotype

  35. Huw says:

    Thank you for this easy as pie tut. Thanks!

  36. Wes Peery says:

    This is great. Works like a charm.

  37. Ryan Lowe says:

    where do i find custom.js? i can’t seem to locate it in your download bundle.

    any help is appreciated :)

  38. Ogi says:

    Yes, Richard, the js folder is missing in the download bundle ;)
    Ryan, the custom.js can be found here: http://www.hv-designs.co.uk/tutorials/jquery/js/custom.js

  39. Superb effect for images. Thanks so much for sharing!

  40. Serg says:

    Great post, very useful, thanks!
    I just have a question.
    Is it possible to use this script if I have a table that has a css specified background that appears on :hover but I want it to gradually fade in? I’m new to javascript so I have no idea how to implement it.
    If I write in just td.image, then it fades out the whole cell with the background AND the text. I want the text to stay but the background of the cell to fade out.

  41. Kelly says:

    I really can;t get this to woerk- tried it 5 times now and no joy and it is definitely linked to the correct CSS sheet. Any ideas? I would love to get this to work.

  42. deppquestcage says:

    Great dude….jquery is superb

  43. Edgaras says:

    Thank you very much.I like your tut’s and designs of webpages.

  44. dxo says:

    Totally rockin’! I cut the code to just one of the function sections, picked an element, and dropped the code in the head of a page and had it working on the first try. So, you’re the man. I’m just getting to know jQuery and all the help I can get is great. Thanks! :)

  45. Lee says:

    Is there a way to apply the fade to more than one div on a page?

    Thanks!!

  46. Stefano says:

    It doesn’t work with LIGHTBOX.
    In my website I have some pages that include lightbox.
    Well, if i try to include fadeinout, even on an image that doesn’t use lightbox, the fade effect does not work.
    Is there a solution?

    Please, let me know

    Regards from Italy
    Stefano

  47. Bee says:

    Hi there, I’m very new to JQuery so I can’t seem to get this to work. Maybe this is a stupid question, but where do I put that code you provided? (The one under ‘new revised code’) – does it go in the custom.js file? And if so, does it go underneath what is already there in this file – http://www.hv-designs.co.uk/tutorials/jquery/js/custom.js … I just don’t understand what I’m doing wrong!

    To whoever replies, thank you in advance for your help. (:

  48. VIKRAM says:

    Hi. thanks for very nice code.
    i would like to ask if it is possible to fade only some child elements while preserving other elements for example in a navigation bar (menus). doing fading on all elements and preserving active one. thanks

  49. Alex says:

    Hello,

    Is there any way to change the color of fadein effect? For example, from white to black?

    Thanks in advance.
    Alex.

  50. Bob Gajewski says:

    Is it possible to fade some text in while fading other text out?

    I have a five-line sentence, and I want the first and second words to fade in while the fourth and fifth words fade out … the third word would always be at 100% opacity.

    Also, I want this to occur on page load and loop continuously – no trigger action (like ‘onClick’) required.

    Thanks in advance …
    Bob

  51. pr00f says:

    Damn it, i search for this for too long … :( THANK YOU !

  52. Ruchi says:

    Hi Richard,

    I am working on a website, which contains a header in flash and rest of the page is in simple html. All the navigation buttons are in flash header. Now I want if I click on any flash button my whole page gets fade out before loading the new page. Although I applied fade in effect in each page but unable to understand how to fade out my page when flash button clicked.

    Thanks in advance for your help.
    Regards
    Ruchi

  53. Aaron says:

    Unfortunately this tutorial is for JQuery and not flash. We don’t do too much with Flash, but I would recommend the Tuts+ network for Flash related tutorials.

  54. Casper says:

    Hi,

    Does anyone knows how i can fade in an pattern on the image. Where do i should place the image pad on?

  55. Jake says:

    Extremely simple, extremely useful. Thanks for this!

  56. Ian says:

    I used this as a base.. but sounds a little too tricky to me..
    here a version which don’t need to apply classes to each element but give opacity effect to all the images in a div with a specified ID

    $(document).ready(function(){
    $(‘img’, ‘#id_of_your_div’).css(“opacity”,”0.4″); // opacity to images on page load
    $(‘img’, ‘#id_of_your_div’).mouseover(function (
    $(this).stop().animate({opacity: 1.0}, 250);
    }).mouseout(function () {
    $(this).stop().animate({opacity: 0.4}, 100);
    });
    });

    thanxs for inspiration
    Ian

  57. TC says:

    Good post.
    both…previous & present one
    …… Thanks
    TC

  58. Fucking Genius dude!

  59. Luca says:

    Thanks Dude, you are really cool. It works well and I used this effect on a website for one of my customers. Cheers from Zurich and thanks a lot!!!

    Luca

  60. mike ilz says:

    Thanks for the script. I actually used it to create an image overlay effect for thumbnails used in an image gallery. http://wp.me/p11izc-DX

  61. enursa says:

    nice. Really working on my site. Thanks.

  62. Nicholas says:

    No matter what, I am not able to get this code working for me. I even copied and pasted the source and linked to the .js and .css files that are running the demo and still nothing works. My code and the demo’s code are identical. Has anyone else had issues with this at all?

  63. Santosh says:

    Very effective and dashing ..thx for sharing..:X

  64. Darren says:

    Where is the “js/custom.js” file? It’s not included in the download…

Leave a Reply