html - 在某一点开始 CSS 过渡

标签 html css 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>

最佳答案

有一些工作需要重新构建 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以及。 :)

关于html - 在某一点开始 CSS 过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34992802/

相关文章:

css - 在使用 google orgchart API 时是否有向特定节点添加类的方法?

css - Material-UI:删除 TimelineItem missingOppositeContent:before 元素

html - 使用 flexbox 的响应式两列布局

CSS3 背景图像过渡

CSS 转换过渡无法正常工作(Chrome)

javascript - 重新调查 "Trying to make different HTML paragraph show depending on selection"

javascript - 请求 header 不是从 Service Worker 发送的

javascript - 如何阻止页面之间简单的 javascript 冲突?

php - 通过网站从 PHP 服务器下载文件

javascript - 无法在 Firefox 中使用 jquery 获取 CSS 过渡属性