在悬停和取消悬停时过渡

时间:2018-05-23 05:36:02

标签: css

大家好!我有悬停和取消悬停的问题。当有人在元素上盘旋时,必须出现一个下拉菜单。它看起来很优雅但却不合时宜地消失了。下面是CSS代码。

.active-box .dropup-content {
  visibility: hidden;
  position: absolute;
  color: rgb(59, 151, 74);
  border-radius: 3px;
  width: 700px;
  bottom: 5em;
  height: 250px;
  z-index: 3;
  margin: 0;
  background-color: rgb(66, 67, 66);
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  transition: all ease-out;
  transition-duration: .2s;
}

.dropup:hover .dropup-content {
  visibility: visible;
  cursor: pointer;
}

1 个答案:

答案 0 :(得分:0)

使用transition: ease-in-out可以提供帮助。请查看以下代码。



<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="http://akashshivanand.com">

  <title>Dropdown</title>

  <!-- Custom styles for this template -->
  <style type="text/css">
    .sub-menu-parent {
      position: relative;
    }
    
    .sub-menu {
      visibility: hidden;
      /* hides sub-menu */
      opacity: 0;
      position: absolute;
      top: 100%;
      left: 0;
      width: 100%;
      transform: translateY(-2em);
      z-index: -1;
      transition: all 0.3s ease-in-out 0s, visibility 0s linear 0.3s, z-index 0s linear 0.01s;
    }
    
    .sub-menu-parent:focus .sub-menu,
    .sub-menu-parent:focus-within .sub-menu,
    .sub-menu-parent:hover .sub-menu {
      visibility: visible;
      /* shows sub-menu */
      opacity: 1;
      z-index: 1;
      transform: translateY(0%);
      transition-delay: 0s, 0s, 0.3s;
      /* this removes the transition delay so the menu will be visible while the other styles transition */
    }
    /* presentational */
    
    body {
      padding: 2%;
      font: 18px/1.4 sans-serif;
    }
    
    nav a {
      color: #E00;
      display: block;
      padding: 0.5em 1em;
      text-decoration: none;
    }
    
    nav a:hover {
      color: #F55;
    }
    
    nav ul,
    nav ul li {
      list-style-type: none;
      padding: 0;
      margin: 0;
    }
    
    nav>ul {
      background: #EEE;
      text-align: center;
    }
    
    nav>ul>li {
      display: inline-block;
      border-left: solid 1px #aaa;
    }
    
    nav>ul>li:first-child {
      border-left: none;
    }
    
    .sub-menu {
      background: #DDD;
    }
  </style>
</head>


<body>

  <nav>
    <ul>
      <li class="sub-menu-parent" tab-index="0">
        <a href="http://google.com">Menu Item 1</a>
        <ul class="sub-menu">
          <li><a href="#">Sub Item 1</a></li>
          <li><a href="#">Sub Item 2</a></li>
          <li><a href="#">Sub Item 3</a></li>
          <li><a href="#">Sub Item 4</a></li>
        </ul>
      </li>
      <li class="sub-menu-parent" tab-index="0">
        <a href="#">Menu Item 2</a>
        <ul class="sub-menu">
          <li><a href="#">Sub Item 1</a></li>
          <li><a href="#">Sub Item 2</a></li>
          <li><a href="#">Sub Item 3</a></li>
          <li><a href="#">Sub Item 4</a></li>
          <li><a href="#">Sub Item 5</a></li>
          <li><a href="#">Sub Item 6</a></li>
        </ul>
      </li>
      <li class="sub-menu-parent" tab-index="0">
        <a href="#">Menu Item 3</a>
        <ul class="sub-menu">
          <li><a href="#">Sub Item 1</a></li>
          <li><a href="#">Sub Item 2</a></li>
        </ul>
      </li>
    </ul>
  </nav>


</body>

</html>
&#13;
&#13;
&#13;