一次启动CSS转换

时间:2016-01-25 12:36:48

标签: html css css3 css-transitions

我使用CSS转换来显示子菜单。主菜单位于右侧列,当用户将鼠标悬停在主菜单上时,子菜单会出现。第一个菜单项从菜单左侧滑出,然后其他菜单项从其中下拉。

我遇到的问题是子菜单需要比主菜单宽,当用户盘旋时,菜单在右边开始太远。我希望菜单以与它移出的速度相同的速度变大,但是在它完成移动之前它将达到最大宽度。我如何实现这个目标?



.mainmenu ul li ul{
  margin-left: 0;
  max-height:61px;
  width:263px;
  overflow: hidden;
  -webkit-transition: margin-left 0.3s ease-in 1s, max-height 1s ease-in;
  -moz-transition: margin-left 0.3s ease-in 1s, max-height 1s ease-in;	
  -o-transition: margin-left 0.3s ease-in 1s, max-height 1s ease-in;	
  -ms-transition: margin-left 0.3s ease-in 1s, max-height 1s ease-in;
  transition: margin-left 0.3s ease-in 1s, max-height 1s ease-in, width 0s ease-in 1s;
}
.mainmenu li:hover ul{
  margin-left: -262px;
  width:263px;
  max-height: 999px;
  -webkit-transition: margin-left 0.3s ease-in, max-height 1s ease-in 0.3s;
  -moz-transition: margin-left 0.3s ease-in, max-height 1s ease-in 0.3s;
  -o-transition: margin-left 0.3s ease-in, max-height 1s ease-in 0.3s;
  -ms-transition: margin-left 0.3s ease-in, max-height 1s ease-in 0.3s;
  transition: margin-left 0.3s ease-in, max-height 1s ease-in 0.3s, width 0s ease-in;
}
#mobile_menu .mainmenu li ul{	
  display: block;
  position:static;
  margin: 0;
  max-height: 999px;
}

<div id="mobile_menu">
  <div class="mainmenu">
    <ul>
      <li><a href='/home-0.html' class='active'  >Home</a></li>
      <li><a href='/blog-25.html' class=''  >Blog</a></li>
      <li>
        <a href='/contact-us-8.html' class=''  >Contact Us</a>
        <ul>
          <li><a href='/contact-us/find-us-27.html' >Find Us</a></li>
          <li><a href='/contact-us/about-us-28.html' >About Us</a></li>
          <li><a href='/contact-us/meet-the-team-29.html' >Meet the Team</a></li>
        </ul>
      </li>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

有一些工作,不得不重组CSS,但它基本上如下:

HTML:

<div id="mobile_menu">
    <div class="mainmenu">
        <ul>
            <li><a href='#' class='active'>Home</a></li>
            <li><a href='#' class=''>Blog</a></li>
            <li>
                <a href='#' class=''>Contact Us</a>
                <ul>
                    <li><a href='#'>Find Us</a></li>
                    <li><a href='#'>About Us</a></li>
                    <li><a href='#'>Meet the Team</a></li>
                </ul>
            </li>
        </ul>
    </div>
</div>

CSS:

#mobile_menu .mainmenu {
    width: 100px;
}
#mobile_menu .mainmenu ul {
    padding: 0;
    width: 100px;
}
#mobile_menu .mainmenu ul li {
    list-style: none;
}
#mobile_menu .mainmenu ul li ul {
    height: 18px;
    margin-left: 0;
    margin-top: -18px;
    overflow: hidden;
    -webkit-transition: height 1s 1s, margin-left 1s, width 1s;
    -moz-transition: height 1s 1s, margin-left 1s, width 1s;
    -o-transition: height 1s 1s, margin-left 1s, width 1s;
    transition: height 1s 1s, margin-left 1s, width 1s;
    width: 0;
}
#mobile_menu .mainmenu ul li:hover ul {
    height: 100px;
    margin-left: 100px;
    width: 100%;
}

......当然还有JSfiddle。 :)