[Free Code Snippets + Explanation] Fading Tab Effect

WhenDeerAttack's picture
So I liked the effect of showing/hiding tabs, but I felt a certain flair was missing from them. So, I wrote some code to allow the content in the container to fade in and out as it changes. I was asked about it today, so I figured I'd share so others can use it, though beware it works a little differently than your usual method of changing the display property. Read on for code snippets, or to learn how to do it yourself.

Working Example







This is the default text that will show up.










Code Snippets


HTML/JS




CSS




JS Only



Explanation/Do It Yourself


Now for a breakdown of how this all works... be warned, this explanation assumes you have at least a basic understanding of Javascript (what functions are, how to style divs using JS, etc). So, to run this code you'll need to go through a few steps. First, in your style tags, you'll need two CSS animations, one to fade elements out and one to fade elements in, like this:


@keyframes fadeout
{0% {opacity:1;}
100% {opacity:0;}
} 
@keyframes fadein
{0% {opacity:0;}
100% {opacity:1;}
}

Then it's time to write the javascript function that runs when you click each link. In these functions, we need to do to do the following things:

  1. Add the fade out animation to the div containing the text
  2. Make sure the Inner HTML of the main text div (named "section0" in this example), gets swapped out for the text in the other, hidden divs.
  3. Add the fade out animation to the div containing the text
  4. Add a timeout to the function so the animations are delayed and run smoothly (not doing this will result in the fading effect playing first, THEN the text swapping out, which is not what we want).
So, our JS will look something like this:
//this makes the old text fade out
setTimeout(function(){document.getElementById('textcon').style.animation='fadeout 0.5s ease';  

//this swaps the text from one section for the text in another. 
setTimeout(function(){ document.getElementById('section0').innerHTML=document.getElementById('section1').innerHTML; 

//this makes the new text fade in
document.getElementById('textcon').style.animation='fadein 0.5s ease'; 

//this is the timeout delay. I've found 500 works best for this effect, but feel free to tweak and adjust to your liking 
until you find something that looks good. The duration of the CSS animations can also be changed.
}, 500); 

}, 500); 


And there you have it. If you need any further explanations or run into any trouble while running this code, feel free to ask. If you're unfamiliar with the concepts I'm laying out, check the links below.

Other Resources


Change the style of a HTML element using JS
The basics of CSS animations
Delay functions using setTimeout

If you ever get stuck, or the code isn't working, remember to check your browser console for errors!


How to use the browser console (Google Chrome) -- this will let you know if there's any errors in your code
How to use the browser console in firefox



Feel free to use, modify, and butcher this code to your liking. Credit isn't needed, but it is appreciated. Thanks for reading!


Omfg I have been TRYING to do

Omfg I have been TRYING to do something like this for years and failing, thank you SO MUCH for sharing this!!! ♥

track!!

track!!
Unplugged's picture

Thanks for sharing! I hope

Thanks for sharing! I hope this catches on. Cool to see a different approach.

(There's some missing brackets in some of the textareas btw)
WhenDeerAttack's picture

C!ssy - No problem, glad I

C!ssy - No problem, glad I could help. Smiling
Postmortem - <3
Unplugged - Thanks! Your code snippets were part of what inspired me to share my own, heh. And drat, figured there would be writing this up while sleep deprived; those should hopefully be fixed now.
Kamaya's picture

~Tracking~ Managed to do it

~Tracking~
Managed to do it earlier, thank you for all the explanation.
Told you some members would want!
HoneyDiva's picture

THANK YOU SO MUCH !!!

THANK
YOU
SO
MUCH
!!!
Aivilo's picture

Dang, I was literally looking

Dang, I was literally looking for exactly this last night. Thank you!!
WhenDeerAttack's picture

You're welcome. ^^

You're welcome. ^^
Eq's picture

ohh what is this o.o shinies

ohh what is this o.o shinies