I have 3 buttons to change to a different visible div with this code, maybe later I would add more buttons to change the divs.
The Current active is the first div (set display block), all other divs are hidden (set display none)
So if I clicked a button, the active div changes based on the button clicked.
function switchUnit1() {
if (document.getElementById('units2')) {
if (document.getElementById('units2').style.display == 'block') {
document.getElementById('units2').style.display = 'none';
document.getElementById('units1').style.display = 'block';
}
}
if (document.getElementById('units3')) {
if (document.getElementById('units3').style.display == 'block') {
document.getElementById('units3').style.display = 'none';
document.getElementById('units1').style.display = 'block';
}
}
if (document.getElementById('units4')) {
if (document.getElementById('units4').style.display == 'block') {
document.getElementById('units4').style.display = 'none';
document.getElementById('units1').style.display = 'block';
}
}
}
function switchUnit2() {
if (document.getElementById('units1')) {
if (document.getElementById('units1').style.display == 'block') {
document.getElementById('units1').style.display = 'none';
document.getElementById('units2').style.display = 'block';
}
}
if (document.getElementById('units3')) {
if (document.getElementById('units3').style.display == 'block') {
document.getElementById('units3').style.display = 'none';
document.getElementById('units2').style.display = 'block';
}
}
if (document.getElementById('units4')) {
if (document.getElementById('units4').style.display == 'block') {
document.getElementById('units4').style.display = 'none';
document.getElementById('units2').style.display = 'block';
}
}
}
function switchUnit3() {
if (document.getElementById('units1')) {
if (document.getElementById('units1').style.display == 'block') {
document.getElementById('units1').style.display = 'none';
document.getElementById('units3').style.display = 'block';
}
}
if (document.getElementById('units2')) {
if (document.getElementById('units2').style.display == 'block') {
document.getElementById('units2').style.display = 'none';
document.getElementById('units3').style.display = 'block';
}
}
if (document.getElementById('units4')) {
if (document.getElementById('units4').style.display == 'block') {
document.getElementById('units4').style.display = 'none';
document.getElementById('units3').style.display = 'block';
}
}
}
<div class='units-buttons' id='units-buttons'>
<button class='units-button units-selected' onclick='switchUnit1();' type='button' value='Click'>Type 36A</button>
<button class='units-button' onclick='switchUnit2();' type='button' value='Click'>Type 36B</button>
<button class='units-button' onclick='switchUnit3();' type='button' value='Click'>Type 56</button>
<div id='units1' style='display: block;'>Image 1</div>
<div id='units2' style='display: none;'>Image 2</div>
<div id='units3' style='display: none;'>Image 3</div>
I want to add a fading transition from 1 div to another every time a button is clicked.
2
Answers
Try
opacity
andz-index
withtransition
to reach fade in/out animation.Set
position: absolute;top: 0;
on.units
make sure they are in same position.For this to work you’d need to use something else than
display
to change which divs are visible, astransition
doesn’t supportdisplay
.But since you want it to fade in anyways, you could use
opacity
instead, which, when transitioned, will make it fade in nicely. I’d also recommend toggling classes on the divs instead of setting styles directly.Here is an example (I also allowed myself to modify your code a bit to make it more expandable and hopefully more readable):