Subscribe Via RSS

3174 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 Stars10 Votes, Rating: 4.5
Loading ... Loading ...
About The Author

About The Author: Richard Carpenter

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

 

Richard Carpenter has written 322 posts.

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

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

18 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 :)

Leave a Reply