为什么我的过渡不起作用?

时间:2017-03-08 20:35:14

标签: css css-transitions

如何让我的过渡工作?从我看过的其他例子来看,它看起来应该有效。我想在子菜单的显示上略微延迟,以便在鼠标滑过它们时立即显示它们。

.TopMenuBar {
  border: none;
  background-color: purple;
  width: 100%;
  margin: 0;
  padding: 0;
}

.TopMenuBar>ul {
  display: flex;
  justify-content: space-between;
  margin: 0;
  padding: 0;
}

.TopMenuBar>ul,
.dropdown-menu>ul,
.sub-dropdown-menu>ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.TopMenuBar>li {
  display: inline;
}

.TopMenuBar a,
.dropdown-menu a,
.sub-dropdown-menu a {
  color: white;
  text-decoration: none;
  padding: 20px;
  display: block
}


/* Applys to all links under class TopMenuBar on hover */

.dropdown-menu,
.sub-dropdown-menu {
  display: none;
  background-color: purple;
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
  position: absolute;
  z-index: 1;
}


/* Applys to all links under class TopMenuBar */

.dropdown-menu a {
  color: white;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown:hover .dropdown-menu {
 display: block;
 transition: all 1s linear 1s;
}

.sub-dropdown {
  position: relative;
}

.sub-dropdown-menu {
  left: 100%;
  top: 0;
  white-space: nowrap;
}
<div class="TopMenuBar">
  <ul>
    <li><a href="#">Home</a></li>
    <li class="dropdown"><a href="#">Programs</a>
      <div class="dropdown-menu">
        <ul>
          <li><a href="#">Preschool Mandarin</a>
            <li><a href="#">Grade School Mandarin</a></li>
            <li><a href="#">High School Mandarin</a></li>
            <li><a href="#">Weekend Class</a></li>
            <li><a href="#">Summer Camp</a></li>
            <li class="sub-dropdown"><a href="#">Examination Training</a>
              <div class="sub-dropdown-menu">
                <ul>
                  <li><a href="#">AP Test</a></li>
                  <li><a href="#">YCT Test</a></li>
                  <li><a href="#">HSK Test</a></li>
                </ul>
              </div>
            </li>
            <li class="sub-dropdown"><a href="#">Adult Mandarin</a>
              <div class="sub-dropdown-menu">
                <ul>
                  <li><a href="#">Conversational Mandarin Class</a></li>
                </ul>
              </div>
            </li>
        </ul>
      </div>
      </li>
      <li class="dropdown"><a href="#">Resources</a>
        <div class="dropdown-menu">
          <ul>
            <li><a href="#">Chinese Club</a></li>
            <li><a href="#">Literature</a></li>
          </ul>
        </div>
      </li>
      <li class="dropdown"><a href="#">Contact us</a>
        <div class="dropdown-menu">
          <ul>
            <li><a href="#">Customer Service</a></li>
            <li><a href="#">Careers</a></li>
          </ul>
        </div>
      </li>
      <li class="dropdown"><a href="#">Registration</a>
  </ul>
</div>

1 个答案:

答案 0 :(得分:0)

如果元素是display: none;,那么转换不起作用就是使用visibility:hidden;opacity: 0;

最好定义

transition: all 1s linear 1s;

关于元素本身而不仅仅是:hover state

像这样:

.TopMenuBar {
  border: none;
  background-color: purple;
  width: 100%;
  margin: 0;
  padding: 0;
}

.TopMenuBar>ul {
  display: flex;
  justify-content: space-between;
  margin: 0;
  padding: 0;
}

.TopMenuBar>ul,
.dropdown-menu>ul,
.sub-dropdown-menu>ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.TopMenuBar>li {
  display: inline;
}

.TopMenuBar a,
.dropdown-menu a,
.sub-dropdown-menu a {
  color: white;
  text-decoration: none;
  padding: 20px;
  display: block
}


/* Applys to all links under class TopMenuBar on hover */

.dropdown-menu,
.sub-dropdown-menu {
  visibility: hidden;
  opacity: 0;
  background-color: purple;
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
  position: absolute;
  z-index: 1;
  
  transition: all 1s linear 1s;
}


/* Applys to all links under class TopMenuBar */

.dropdown-menu a {
  color: white;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown:hover .dropdown-menu {
  visibility: visible;
  opacity: 1;
}

.sub-dropdown {
  position: relative;
}

.sub-dropdown-menu {
  left: 100%;
  top: 0;
  white-space: nowrap;
}
<div class="TopMenuBar">
  <ul>
    <li><a href="#">Home</a></li>
    <li class="dropdown"><a href="#">Programs</a>
      <div class="dropdown-menu">
        <ul>
          <li><a href="#">Preschool Mandarin</a>
            <li><a href="#">Grade School Mandarin</a></li>
            <li><a href="#">High School Mandarin</a></li>
            <li><a href="#">Weekend Class</a></li>
            <li><a href="#">Summer Camp</a></li>
            <li class="sub-dropdown"><a href="#">Examination Training</a>
              <div class="sub-dropdown-menu">
                <ul>
                  <li><a href="#">AP Test</a></li>
                  <li><a href="#">YCT Test</a></li>
                  <li><a href="#">HSK Test</a></li>
                </ul>
              </div>
            </li>
            <li class="sub-dropdown"><a href="#">Adult Mandarin</a>
              <div class="sub-dropdown-menu">
                <ul>
                  <li><a href="#">Conversational Mandarin Class</a></li>
                </ul>
              </div>
            </li>
        </ul>
      </div>
      </li>
      <li class="dropdown"><a href="#">Resources</a>
        <div class="dropdown-menu">
          <ul>
            <li><a href="#">Chinese Club</a></li>
            <li><a href="#">Literature</a></li>
          </ul>
        </div>
      </li>
      <li class="dropdown"><a href="#">Contact us</a>
        <div class="dropdown-menu">
          <ul>
            <li><a href="#">Customer Service</a></li>
            <li><a href="#">Careers</a></li>
          </ul>
        </div>
      </li>
      <li class="dropdown"><a href="#">Registration</a>
  </ul>
</div>