I’m trying to make a contact icon which, when pressed moves to the side, revealing the contact information. I would like the text to remain centred and for the contact info to fade in. So far I have this:
<div class="contacts">
<div class="contactIcon">ICON<span class="contactInfo"> (+xx)xxxxxxx</span></div>
<div class="contactIcon">ICON<span class="contactInfo"> (+xx)xxxxxxx</span></div>
</div>
let contactIcon = document.querySelectorAll('.contactIcon');
for (let i = 0; i < contactIcon.length; i++) {
contactIcon[i].addEventListener('click', function () {
let contactInfo = contactIcon[i].querySelector('.contactInfo');
if (contactInfo.style.display == 'none') {
contactInfo.style.visibility = 'visible'
contactInfo.style.display = 'inline';
contactInfo.style.maxWidth ='100px';
}
else {
contactInfo.style.visibility = 'hidden';
contactInfo.style.display = 'none';
contactInfo.style.maxWidth = '0';
}
});
.contactIcon {
overflow: hidden;
}
.contactInfo {
visibility: hidden;
display: none;
opacity: 1;
max-width: 500px;
transition: visibility 0s, opacity 0.3s, max-width 0.6s linear;
}
The code works to a degree, the icon shifts to allow space, but there is no animation and I’m not sure why, I’ve tried toggling a ‘.hide’ class, but also couldn’t get that to work. Also it takes 2 clicks to make the content appear, and I’m struggling to understand that also.
2
Answers
The latter consern is easy to answer:
contactInfo.style.display == 'none'
only detects inline styles, so you must removedisplay:none;
from css and attach it to htmlspan
. as for animations,display: none
spoils everything here, chooseopacity
orwidth
to make element visually disappear.p.s. I attached inline style to make you see, how it has been detected with
contactInfo.style.opacity == "0"
here is better way without inline style:try this