I'm having trouble trying to figure out why if you click the drop-down arrow too quickly, the options in the drop-down list are saved even after it is closed.
HTML
<div class="dropdown-container">
<div class="dropdown-header">
<h5 class="js-title-hook dropdown-title">
Option 1
</h5>
<div class="toggle-button">
<p>
↓
</p>
</div>
</div>
<div class="dropdown-options">
<ul class="dropdown-options-list">
<li class="js-hook-dropdown-list" data-title="Option 1">Option 1</li>
<li class="js-hook-dropdown-list" data-title="Option 2">Option 2</li>
<li class="js-hook-dropdown-list" data-title="Option 3">Option 3</li>
<li class="js-hook-dropdown-list" data-title="Option 4">Option 4</li>
<li class="js-hook-dropdown-list" data-title="Option 5">Option 5</li>
</ul>
</div>
</div>
CSS
.dropdown-container{
width: 400px;
font-family: 'helvetica';
color: 2b2b2b;
border-bottom: 1px solid #2b2b2b;
}
.dropdown-title{
float: left;
margin-right: 20px;
}
.toggle-button{
display: inline-block;
cursor: pointer;
transform: rotate(0deg);
}
.dropdown-options{
}
.dropdown-options{
visibility: hidden;
opacity: 0;
transition: height 1s, visibility 0.5s, opacity 0.5s ease-out;
}
.dropdown-options-list{
list-style: none;
}
.dropdown-options-list li{
margin-bottom: 5px;
cursor: pointer;
}
.dropdown-options-list li:hover{
color: #D62727;
}
Javascript
(function(){
var DOMstrings = {
dropPointer: '.toggle-button',
pageTitle: '.js-title-hook',
dropOptions: '.dropdown-options',
dropTitle: '.project-dropdown-title',
listItems: '.js-hook-dropdown-list',
}
var dropPointer = document.querySelector(DOMstrings.dropPointer);
var dropOptions = document.querySelector(DOMstrings.dropOptions);
var dropTitle = document.querySelector(DOMstrings.dropTitle);
var height = dropOptions.clientHeight;
var width = dropOptions.clientWidth;
dropOptions.style.height = '0';
dropDown();
function dropDown(){
dropPointer.addEventListener('click', function(){
if(dropPointer.style.transform !== "rotate(180deg)"){
dropPointer.style.transform = "rotate(180deg)";
setTimeout(function(){
dropOptions.style.height = height + 'px';
dropOptions.style.marginBottom = '10px';
}, 150)
setTimeout(function(){
dropOptions.style.visibility = 'visible';
dropOptions.style.opacity = '1';
}, 700)
} else {
closeDropdown();
}
});
}
function closeDropdown() {
dropPointer.style.transform = "rotate(0deg)";
dropOptions.style.visibility = 'hidden';
dropOptions.style.opacity = '0';
setTimeout(function(){
dropOptions.style.height = '0';
dropOptions.style.marginBottom = '0px';
}, 200);
setTimeout(function(){
dropTitle.style.marginBottom = "0px";
}, 500)
}
var elements = document.querySelectorAll(DOMstrings.listItems);
var title = document.querySelector(DOMstrings.pageTitle);
for(var i = 0; i < elements.length; i++){
changeProject(elements[i]);
}
function changeProject(el) {
el.addEventListener('click', function(e){
var target = e.currentTarget;
title.innerHTML = target.dataset.title;
closeDropdown();
})
}
})();
The code was a bit long, so here is a JSfiddle that illustrates the behavior: https://jsfiddle.net/andylyell/jkjxmLxx/
Thanks!
source
share