【问题标题】:How to prevent a transition delay如何防止过渡延迟
【发布时间】:2015-07-16 00:00:37
【问题描述】:

我一直在为jsfiddle 找到的导航菜单使用 CSS 过渡。相同的代码可以在下面找到:

编辑:Updated jsfiddle link

CSS:

nav {
    background-color: #337AB7;
    position: relative;
}

nav:after {
    display: table;
    content: '';
    clear: both;
}

nav h1 {
    float: left;
    margin: 0;
}

nav #nav-toggle {
    display: none;
}

nav label[for='nav-toggle']:after {
    float: right;
    display: block;
    padding: .5em;
    margin: .5em;
    width: 4em;
    content: 'Menu';
    text-align: center;
    color: #fff;
    border: 1px solid #fff;
    border-radius: 3px;
}

nav ul {
    float: left;
    display: block;
    margin: 0;
    padding: 0;

    list-style: none;
    width: 100%;
    max-height: 0;
    overflow: hidden;



    transition: max-height 1s ease;
}

nav #nav-toggle:checked ~ ul {
    max-height: 1000px;

    transition: max-height 1s ease;
}

nav #nav-toggle:checked ~ label:after {
    content: 'Close';
    background-color: #fff;
    color: #337AB7;
}

nav li {
    margin: .5em 0;
}

nav a {
    display: block;
    padding: .5em;
    text-decoration: none;
    color: #fff;
}

nav a:hover,
nav a:active,
nav a:focus {
    background-color: #fff;
    color: #337AB7;
}

HTML:

<nav>
   <h1><a href="#">Title</a></h1>
   <input type="checkbox" id="nav-toggle">
   <label for="nav-toggle"></label>
   <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact</a></li>
   </ul>
</nav>

开始的过渡工作正常,但在关闭菜单时,在过渡实际发生之前会有某种延迟。如何停止这种延迟?

【问题讨论】:

    标签: css delay transition


    【解决方案1】:

    您的动画需要这么长时间的原因是您正在转换max-height。即使您的最终高度只有 134 像素,您也不会转换它。它正在为您设置 max-height 的全部 1000px 进行转换。当它展开时,这看起来没有任何问题,但当它关闭时,它从 1000 开始并倒计时。看起来它正在被延迟,即使它不是。在达到大约 134 像素之前,它不会对实际高度产生影响。如果将其更改为高度和过渡,则问题已解决。只需将您的身高降低到更易于管理的水平即可。

    当您使用height 而不是max-height 时,您会看到问题:

    nav {
      background-color: #337AB7;
      position: relative;
    }
    nav:after {
      display: table;
      content: '';
      clear: both;
    }
    nav h1 {
      float: left;
      margin: 0;
    }
    nav #nav-toggle {
      display: none;
    }
    nav label[for='nav-toggle']:after {
      float: right;
      display: block;
      padding: .5em;
      margin: .5em;
      width: 4em;
      content: 'Menu';
      text-align: center;
      color: #fff;
      border: 1px solid #fff;
      border-radius: 3px;
    }
    nav ul {
      float: left;
      display: block;
      margin: 0;
      padding: 0;
      list-style: none;
      width: 100%;
      height: 0;
      overflow: hidden;
      transition: height 1s ease;
    }
    nav #nav-toggle:checked ~ ul {
      height: 1000px;
      transition: height 1s ease;
    }
    nav #nav-toggle:checked ~ label:after {
      content: 'Close';
      background-color: #fff;
      color: #337AB7;
    }
    nav li {
      margin: .5em 0;
    }
    nav a {
      display: block;
      padding: .5em;
      text-decoration: none;
      color: #fff;
    }
    nav a:hover,
    nav a:active,
    nav a:focus {
      background-color: #fff;
      color: #337AB7;
    }
    <nav>
      <h1><a href="#">Title</a></h1>
      <input type="checkbox" id="nav-toggle">
      <label for="nav-toggle"></label>
      <ul>
        <li><a href="#">Home</a>
        </li>
        <li><a href="#">About</a>
        </li>
        <li><a href="#">Contact</a>
        </li>
      </ul>
    </nav>

    【讨论】:

    • 我完全错过了。我知道将它设置为一个过大的数字会让我反感。
    • 是的,我仍在尝试找到一种无需设置显式高度即可使其工作的方法。很遗憾,您无法从 height: 0 转换到 height: auto
    【解决方案2】:

    将您的转换代码更改为:transition: max-height .3s ease-in-out;

    这将使其更快,并将动画添加到下拉菜单,而不仅仅是下拉菜单。

    编辑:

    nav ul {
        float: left;
        display: block;
        margin: 0;
        padding: 0;
        list-style: none;
        width: 100%;
        max-height: 0;
        overflow: hidden;
        transition: max-height 100ms ease;
    }
    
    nav #nav-toggle:checked ~ ul {
        max-height: 1000px;
        transition: max-height 1.5s ease;
    }
    

    试试上面的。下拉菜单会产生很好的平滑效果,但返回会很快,但会有不明显的延迟。

    如果这超出了我的范围,则实际原因。我的猜测是其他代码在转换之前被渲染,因为你有一个点击,一个动作,然后将转换添加到动作中。不过这只是我的猜测。

    【讨论】:

    • 谢谢,这确实使结束动画更好。但是在关闭之前仍然有一个微小的停顿,这在打开时不会发生。添加了更新的 jsfiddle 链接
    • 是的,我也看到了。我会尽快找到修复和更新。
    【解决方案3】:

    Updated fiddle - 仅修复 1 行

    修复延迟解决方案:

    为元素放置cubic-bezier(0, 1, 0, 1)转换函数。

    .text {
      overflow: hidden;
      max-height: 0;
      transition: max-height 0.5s cubic-bezier(0, 1, 0, 1);
      &.full {
        max-height: 1000px;
        transition: max-height 1s ease-in-out;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多