Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Fading elements in and out is also a common effect that you see in modern web pages, and jQuery provides functions for achieving this effect as well. You can fade images or other elements either completely in or out, or you can fade them to a predetermined level of opacity, so that they are transparent. And you can select the speed of how fast things are fading either in or out by using strings like slow, normal, or fast, or as a millisecond duration.
And you can see here in the table there is three routines that perform fading. There is the fadeIn. There is the fadeOut and fadeTo. FadeIn does what its name implies. It fades in all the elements that are in the matched set, that a selector came back with, which is what you're calling this function on, and you can specify the speed that you want this to happen at. And there is an optional callback that will run after the effect is finished. So if you've got something that you want to happen when things are faded either in or out, you can supply a callback function, and that function will get called once the effect is done.
Similarly, there is a fadeOut function. It does the opposite of fadeIn, and here is fadeTo. And for fadeTo, there is a speed and callback function, but there is also the opacity level that you want the object to fadeTo. So, anywhere between zero and 1.0, that will be the percentage of transparency that the object will fadeTo. Let's jump over to the code and see how these work. So, here we have the div element, and nice big blue div, sitting in my document, and I've got a couple of buttons down here, and the buttons will perform various functions.
So, I've got Fade in and Fade Out and then a couple of values for Fade To. So it's already faded in, let's try fading it out. So when you fade it out, you can see it slowly disappears, fading it in makes it slowly appear and we can also fade it down to 30% and we can fade it up to 100%. So, lets take a look at the code and see how this works. I am going to bring this up in my editor, and you bring it up in whatever editor you are using. You can see that there are some jQuery code there that's using the fade functions, but before we take a look at that, let's scroll down here.
You can see here is the element and here are the buttons and I've got my various buttons for fading elements to various states. So let's scroll back up to the code. So, here we are when the document is ready, we assign some event handlers and each button gets a click handler. So, for fading in, we have a fadeIn function and fadeOut, and again, I can control how fast I want these to happen. So I've got some speeds that I specified here, but if I want something to fade out very slowly, instead of using normal, I can specify duration like 4000 and that will fade out after 4 seconds, and I can supply a function that will get called when its done, and let's just write a little alert that says done fading out and save.
Let's go back out to the browser, let's refresh. Again, the fading works. When I click Fade Out it's going to very slowly fade over 4 seconds and my callback, it gets called. So, let's just quickly make sure that this works cross-browser like we say it does, let's bring this up in Firefox. I am going to open this up in the Firefox, and there it is, fading to 30%, fading to 100% and we can fade it in, and let's fade it out and it goes over 4 seconds, here is my alert, let's fade it back in.
That's quick summary of the fading functions.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92480 Viewers
82 Video lessons · 104137 Viewers
71 Video lessons · 75610 Viewers
56 Video lessons · 106983 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.