HTML5 CSS3在<ul>标签上转换为淡入

时间:2018-05-19 06:30:32

标签: html5 css3 css-transitions

我通常不会遇到过渡问题 但是,我正在实施一个汉堡菜单,并希望ul淡入。

以下是代码:

&#13;
&#13;
nav #toggle {
  display: none;
}

nav #navelements {
  display: none;
  transition: .7s;
}

nav #navelements ul li {
  line-height: 2rem;
  text-align: left;
  width: 100%;
}

nav #toggle:checked+#navelements {
  display: block;
  transition: .7s;
}
&#13;
<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <title>Document</title>
  </head>

  <body>
    <nav>
      <h1>
        LOGO
      </h1>
      <label for="toggle">&#9776</label>
      <input type="checkbox" id="toggle">
      <div id="navelements">
        <ul>
          <li><a href="#">home</a></li>
          <li><a href="#">about</a></li>
          <li><a href="#">contact</a></li>
        </ul>
      </div>
    </nav>
  </body>

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

我做错了什么?

2 个答案:

答案 0 :(得分:1)

transition无法使用display&amp; visibility ,因此您必须使用opacity

&#13;
&#13;
nav #toggle {
  display: none;
}

nav #navelements {
  opacity: 0;
  transition: opacity .5s linear;
}

nav #navelements ul li {
  line-height: 2rem;
  text-align: left;
  width: 100%;
}

nav #toggle:checked+#navelements {
  opacity: 1;
}
&#13;
<nav>
  <h1>
    LOGO
  </h1>
  <label for="toggle">&#9776</label>
  <input type="checkbox" id="toggle">
  <div id="navelements">
    <ul>
      <li><a href="#">home</a></li>
      <li><a href="#">about</a></li>
      <li><a href="#">contact</a></li>
    </ul>
  </div>
</nav>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

  

您可以仅使用css3和html 5

尝试这个有效的汉堡包菜单

&#13;
&#13;
body
{
  margin: 0;
  padding: 0;
  background: #0077CC;
  color: #cdcdcd;
  font-family: "Avenir Next", "Avenir", sans-serif;
}

a {
  text-decoration: none;
  color: #232323;
  
  transition: color 0.3s ease;
}

a:hover {
  color: #0077CC;
}

#menuToggle {
  display: block;
  position: relative;
  top: 10px;
  left: 10px;
  z-index: 1;
  
  -webkit-user-select: none;
  -moz-user-select: none;
   -ms-user-select: none;
   user-select: none;
}

#menuToggle input
{
  display: block;
  width: 40px;
  height: 32px;
  position: absolute;
  top: -7px;
  left: -5px;
  cursor: pointer;
  opacity: 0; /* hide this */
  z-index: 2; /* and place it over the hamburger */
  -webkit-touch-callout: none;
}

#menuToggle span {
  display: block;
  width: 33px;
  height: 4px;
  margin-bottom: 5px;
  position: relative;
  background: #cdcdcd;
  border-radius: 3px;
  
  z-index: 1;
  
  -webkit-transform-origin: 4px 0px;
  transform-origin: 4px 0px;
  
  transition: background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
  opacity 0.55s ease,
  -webkit-transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
  
  transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
  background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
  opacity 0.55s ease;
  
  transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
  background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
  opacity 0.55s ease,
  -webkit-transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
}

#menuToggle span:first-child {
  -webkit-transform-origin: 0% 0%;
   transform-origin: 0% 0%;
}

#menuToggle span:nth-last-child(2) {
  -webkit-transform-origin: 0% 100%;
   transform-origin: 0% 100%;
}

#menuToggle input:checked ~ span {
  opacity: 1;
  -webkit-transform: rotate(45deg) translate(-2px, -1px);
   transform: rotate(45deg) translate(-2px, -1px);
  background: #232323;
}

#menuToggle input:checked ~ span:nth-last-child(3) {
  opacity: 0;
  -webkit-transform: rotate(0deg) scale(0.2, 0.2);
   transform: rotate(0deg) scale(0.2, 0.2);
}

#menuToggle input:checked ~ span:nth-last-child(2) {
  -webkit-transform: rotate(-45deg) translate(0, -1px);
   transform: rotate(-45deg) translate(0, -1px);
}

#menu {
  position: absolute;
  width: 300px;
  margin: -100px 0 0 -50px;
  padding: 50px;
  padding-top: 125px;
  height:auto;
  height: 200px;
  background: #ededed;
  list-style-type: none;
  -webkit-font-smoothing: antialiased;
  /* to stop flickering of text in safari */
  
  -webkit-transform-origin: 0% 0%;
  
          transform-origin: 0% 0%;
  -webkit-transform: translate(-100%, 0);
          transform: translate(-100%, 0);
  
  transition: -webkit-transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
  transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
  transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0), -webkit-transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
}

#menu li {
  padding: 5px 0;
  font-size: 22px;
}

#menuToggle input:checked ~ ul {
  -webkit-transform: none;
   transform: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<nav role="navigation">
  <div id="menuToggle">
    <!--
    A  hidden checkbox is used as click reciever,
    so you can use the :checked selector on it.
    -->
    <input type="checkbox" />
    
    <!--
    Some spans to act as a hamburger.
    
    They are acting like a real hamburger,
    -->
    <span></span>
    <span></span>
    <span></span>
    
    <ul id="menu">
      <a href="#"><li>home</li></a>
      <a href="#"><li>About</li></a>
      <a href="#"><li>Contact</li></a>
    </ul>
  </div>
</nav>
&#13;
&#13;
&#13;