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.
This is the first text that will show up. Could be the same as default, could be different. That's up to you.
This is the second text that will show up.
This is the third text that will show up.
This is the fourth text that will show up.
This is the fifth 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:
- Add the fade out animation to the div containing the text
- 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.
- Add the fade out animation to the div containing the text
- 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
track!!
Thanks for sharing! I hope
(There's some missing brackets in some of the textareas btw)
C!ssy - No problem, glad I
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.
~Tracking~ Managed to do it
Managed to do it earlier, thank you for all the explanation.
Told you some members would want!
THANK YOU SO MUCH !!!
YOU
SO
MUCH
!!!
Dang, I was literally looking
You're welcome. ^^
ohh what is this o.o shinies