I'm trying to hide/show elements using JavaScript. Used code from a similar question as reference. Fade out is working like it should, but for some reason, fade in isn't:
function hidePages() {
var pages = document.getElementsByClassName('page');
for(var i = 0; i < pages.length; i++) {
current = pages[i];
current.style.opacity = 0;
setTimeout(function(current) {
current.style.display = 'none';
}, 500, current);
}
}
function navigate(page) {
hidePages();
var current = document.getElementById(page);
setTimeout(function(current) {
current.style.display = 'block';
current.style.opacity = 1;
}, 500, current);
}
.page {
opacity: 1;
-webkit-transition: opacity 0.5s;
-o-transition: opacity 0.5s;
transition: opacity 0.5s;
}
<a href="#" onclick="navigate('page1')">1</a>
<a href="#" onclick="navigate('page2')">2</a>
<a href="#" onclick="navigate('page3')">3</a>
<div class="page" id="page1">Page 1</div>
<div class="page" id="page2" style="display: none">Page 2</div>
<div class="page" id="page3" style="display: none">Page 3</div>
JSFiddle: https://jsfiddle.net/z2svo5uu/
As you can see, as soon as the element has faded out, it shows up immediately. I want to have the fade transition for fading in too, though.
I know I could do this easier with jQuery, but I'm just wondering why the transition isn't working as it should? Anyone know?