Drop-down list error, if you click too quickly, the options in the drop-down list remain visible

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>
        &darr;    
      </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{
  /* padding-top: 30px; */
}

.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(){

  //Incase anything ever changes in the DOM
    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;

    //initisualise the height of drop options
    dropOptions.style.height = '0';

    // console.log(height + ' x ' + width);

    dropDown();

    function dropDown(){
      dropPointer.addEventListener('click', function(){

        //toggle for the pointer rotating
        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)

          // console.log(height + ' x ' + width);

        } 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);

    // console.log(listProjects);

    for(var i = 0; i < elements.length; i++){
      changeProject(elements[i]);
    }

    function changeProject(el) {
      el.addEventListener('click', function(e){

        var target = e.currentTarget;
        // console.log(target);

        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!

+6
source share
2 answers

create a global variable for the setTimeout function, which will make the content visible:

thistimeout = setTimeout(function(){
          dropOptions.style.visibility = 'visible';
          dropOptions.style.opacity = '1';
      }, 700)

clearTimeout() closeDropdown(), setTimeout

function closeDropdown() {
  clearTimeout(thistimeout);
  dropPointer.style.transform = "rotate(0deg)";
  dropOptions.style.visibility = 'hidden';

: https://jsfiddle.net/VLK_STUDIO/jkjxmLxx/2/

(function(){

  //Incase anything ever changes in the DOM
    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;

    //initisualise the height of drop options
    dropOptions.style.height = '0';

    // console.log(height + ' x ' + width);

    dropDown();

    function dropDown(){
      dropPointer.addEventListener('click', function(){

        //toggle for the pointer rotating
        if(dropPointer.style.transform !== "rotate(180deg)"){
          dropPointer.style.transform = "rotate(180deg)";

          setTimeout(function(){
            dropOptions.style.height = height + 'px';
            dropOptions.style.marginBottom = '10px';
          }, 150)

          thistimeout = setTimeout(function(){
              dropOptions.style.visibility = 'visible';
              dropOptions.style.opacity = '1';
          }, 700)

          // console.log(height + ' x ' + width);

        } else {
          closeDropdown();
        }
      });
    }

    function closeDropdown() {
      clearTimeout(thistimeout);
      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);

    // console.log(listProjects);

    for(var i = 0; i < elements.length; i++){
      changeProject(elements[i]);
    }

    function changeProject(el) {
      el.addEventListener('click', function(e){

        var target = e.currentTarget;
        // console.log(target);

        title.innerHTML = target.dataset.title;

        closeDropdown();
      })
    }

})();
+1

setTimeout dropDown().

700 , dropOptions.style.visibility = 'hidden'; , 700 , dropOptions.style.visibility = 'visible'; – .

, setTimeout , / dragOptions.

:

function dropDown(){
  dropPointer.addEventListener('click', function(){

    //toggle for the pointer rotating
    if(dropPointer.style.transform !== "rotate(180deg)"){
      dropPointer.style.transform = "rotate(180deg)";

      setTimeout(function(){
        if(dropPointer.style.transform !== "rotate(180deg)")return;
        dropOptions.style.height = height + 'px';
        dropOptions.style.marginBottom = '10px';
      }, 150)

      setTimeout(function(){
          if(dropPointer.style.transform !== "rotate(180deg)")return;
          dropOptions.style.visibility = 'visible';
          dropOptions.style.opacity = '1';
      }, 700)

      // console.log(height + ' x ' + width);

    } else {
      closeDropdown();
    }
  });
}

function closeDropdown() {
  dropPointer.style.transform = "rotate(0deg)";
  dropOptions.style.visibility = 'hidden';
  dropOptions.style.opacity = '0';

  setTimeout(function(){
    if(dropPointer.style.transform === "rotate(180deg)")return;
    dropOptions.style.height = '0';
    dropOptions.style.marginBottom = '0px';
  }, 200);

  setTimeout(function(){
    if(dropPointer.style.transform === "rotate(180deg)")return;
    dropTitle.style.marginBottom = "0px";
  }, 500);
}

JSFiddle.

+1

Source: https://habr.com/ru/post/1016746/


All Articles