im trying to show hide toggle this submenu button trigger
and was trying to have it animated by opacity and have the display none so that the height adjusts accordingly and smoothly upon open/close.
Need help, Thank you.
Javascript
var dropdown = document.getElementsByClassName("dropdown-btn");
var i;
for (i = 0; i < dropdown.length; i++) {
dropdown[i].addEventListener("click", function () {
this.classList.toggle("active");
var dropdownContent = this.nextElementSibling;
if (dropdownContent.style.opacity === "100" , dropdownContent.style.display === "block"
) {
dropdownContent.style.opacity = "0" , dropdownContent.style.display = "none";
} else {
dropdownContent.style.opacity = "100" , dropdownContent.style.display = "block";
}
});
}
2
Answers
There is a problem with the condition in your code where you are checking if the opacity is equal to 100 and the display is equal to "block" using the comma operator (,). The comma operator will evaluate both expressions and return the result of the last expression, which in this case is the display value.
I am using setTimeout to delay setting the display or opacity property to allow the CSS transition to occur smoothly. When the display property is set to "none", the element is immediately removed from the document flow, and there is no transition animation. NOTE: You can make your own changes in those.
Hope this helps
Try to use opacity and visibility combination instead of with display property in css and doesn’t need a loop for single element, use [0] index in elementsbyclassname selector