JSFiddle - React, Tailwind, and code Playground

by evon0306

HTML

<nav>
   <ul class="nav">
      <li><a href="#">首頁</a></li>
      <li><a href="#">全部商品</a></li>
      <li><a href="#">全部品牌</a></li>
      <li class="about">關於我們 
         <ul class="down">
            <li><a href="#">團隊介紹</a></li>
            <li><a href="#">企業宗旨</a></li>
            <li><a href="#">創業故事</a></li>
         </ul>
      </li>
   </ul>
</nav>

CSS

* {
  list-style: none;
}

nav {
  height: 100%;
  background-color: #F3F4F8;
}
nav .nav {
  display: flex;
  position: relative;
  flex-direction: row;
  align-items: center;
  max-width: 70%;
  margin: 0 auto;
  padding: 0;
  background-color: #907dda;
  border-radius: 6px 6px 0 0;
}
nav .nav li {
  position: relative;
  flex: 1 0;
  height: 60px;
  font-size: 14px;
  text-align: center;
  color: #e7e6f1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
nav .nav li a {
  color: #e7e6f1;
  text-decoration: none;
}
nav .nav li:hover {
  background-color: rgba(0, 0, 0, 0.1);
}
nav .nav li .down {
  position: absolute;
  visibility: hidden;
  display: flex;
  flex-direction: column;
  top: 100%;
  left: 0;
  right: 0;
  padding: 5px 0;
  color: #333;
  background-color: #e5e4ea;
  border-radius: 0 0 6px 6px;
  box-shadow: 1px 2px 5px -1px rgba(0, 0, 0, 0.15), 0px 4px 14px -1px rgba(0, 0, 0, 0.1);
  -webkit-animation: slideInLeft 0.3s ease-in-out backwards;
          animation: slideInLeft 0.3s ease-in-out backwards;
  transition: transform 0.2s ease, opacity 0.2s z-index 0s 0.2s;
  opacity: 0;
  z-index: -1;
}
nav .nav li .down li {
  cursor: pointer;
  padding: 15px;
  color: #333;
}
nav .nav li .down li:hover {
  background-color: rgba(0, 0, 0, 0.06);
}
nav .nav li .down li a {
  font-size: 14px;
  color: #333;
}

@-webkit-keyframes slideInLeft {
  from {
    transform: translate(-25%, 0);
    opacity: 0;
  }
  to {
    transform: translate(0, 0);
    opacity: 1;
  }
}

@keyframes slideInLeft {
  from {
    transform: translate(-25%, 0);
    opacity: 0;
  }
  to {
    transform: translate(0, 0);
    opacity: 1;
  }
}

JavaScript

let about = document.querySelector(".about")
about.onclick = open;
about.onmouseleave = out;


function open(){
   let openDown = document.querySelector(".down")
   openDown.style.visibility = "visible";
   openDown.style.transform = "translate(0,0)";
   openDown.style.opacity = "1";
   openDown.style["z-index"] = "0";   
}
function out(){
   let openDown = document.querySelector(".nav li ul");
   openDown.style.visibility = "hidden";
   openDown.style.opacity = "0";
   openDown.style["z-index"] = "1";
}