Login Or Sign up

How to slow down dropdown box by CSS or jQuery

869 Views Asked by At

I have built a dropdown button,but have no idea how to slow the the dropping process:

Html:

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">下拉菜单</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
  </div>
</div>

CSS:

.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #f1f1f1}

.show {display:block;}

JS:

/* click to show the box */
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// click out area to call back the box
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}

So my question is how can I slow down the process of this function so that the whole process looks more smoothly.

I have tried to add:

$("div#myDropdown").slideDown(1000);

in front of:

document.getElementById("myDropdown").classList.toggle("show");

But this makes me can't hide the menu when I click out area.Any friend can help?

1

There are 1 best solutions below

0
cloned On

$('#myDropdown').slideDown(); is a jQuery function, if you want to use this effect you have to also include jQuery.

In this slideDown (and also slideUp) you can specify a callback function which get's executed once the animation finished playing. After the animation finished playing I set toggle your classes (to have the otherwise same functionality as you currenctly have)

function myFunction() {
 $('#myDropdown').slideDown(200);
  document.getElementById("myDropdown").classList.toggle("show");
}

// click out area to call back the box
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        $('#myDropdown').slideUp(200, function() {
          openDropdown.classList.remove('show');
        });
      }
    }
  }
}
.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover,
.dropbtn:focus {
  background-color: #3e8e41;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {
  background-color: #f1f1f1
}

.show {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">下拉菜单</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
  </div>
</div>