如何计算 slideToggle() 的速度

How to calculate a speed for slideToggle()

我有一个包含多个 accordion-style 项的导航菜单。有些 "parent" 链接比其他链接 children 多。我想改变 slideToggle() 的速度,以便具有更多 children 的那些需要更长的时间才能到达 slideDown()。这是我尝试过的,但由于某种原因它跳来跳去。如您所见,根本没有放松。

// Get the height of each list and save it in the data-height attribute
$('.main-nav > ul > li > ul').each(function() {
  $(this).slideDown(0);
  $(this).data('height', $(this).height());
  $(this).slideUp(0);
});

$('.main-nav > ul > li').click(function() {
  // Multiply the height of the element by the speed desired
  $(this).children().slideToggle($(this).data('height') * 1000, 'easeInOutExpo');
});
.main-nav {
  position: fixed;
  top: 0;
  left: 0;
}
.main-nav ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
  font-size: 18px;
}
.main-nav ul li {
  padding: 22px 0;
  cursor: pointer;
}
.main-nav > ul {
  padding: 0 22px;
}
.main-nav > ul > li > ul {
  display: none;
}
*,
*:before,
*:after {
  box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
<nav class="main-nav">
  <ul>
    <li>Global
      <ul>
        <li>Typography</li>
        <li>Colors</li>
        <li>Icons</li>
      </ul>
    </li>
    <li>Elements
      <ul>
        <li>Text</li>
        <li>Buttons</li>
        <li>Lists</li>
        <li>Tables</li>
        <li>Media</li>
      </ul>
    </li>
    <li>Controls
      <ul>
        <li>dropdown</li>
        <li>alerts</li>
        <li>badges</li>
        <li>modals</li>
      </ul>
    </li>
    <li>Layout
      <ul>
        <li>dynamic row</li>
        <li>flex</li>
      </ul>
    </li>
    <li>Components
      <ul>
        <li>cards</li>
        <li>banners</li>
        <li>itemEditor</li>
        <li>itemIndex</li>
        <li>jQueryUI</li>
        <li>login</li>
        <li>main</li>
        <li>details (and detail views)</li>
        <li>drilldown</li>
        <li>mega menu</li>
        <li>navigation</li>
        <li>search</li>
        <li>thick items</li>
        <li>widgets</li>
      </ul>
    </li>
  </ul>
</nav>

$(this).data('height') 似乎未定义,因此您最终似乎将 NaN 作为第一个参数传递给 slideToggle() 函数,我不确定修复方法是什么但是希望这能为您指明正确的方向

我建议不要基于高度,而是基于 ul 中有多少 li 元素(如果 1000 对您来说太慢,您可能需要修改乘数).

// Get the height of each list and save it in the data-height attribute
$('.main-nav > ul > li > ul').each(function() {
  $(this).slideUp(0);
});

$('.main-nav > ul > li').click(function() {
  // Multiply the height of the element by the speed desired
  $(this).children().slideToggle($(this).find("li").length * 1000, 'easeInOutExpo');
});
.main-nav {
  position: fixed;
  top: 0;
  left: 0;
}
.main-nav ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
  font-size: 18px;
}
.main-nav ul li {
  padding: 22px 0;
  cursor: pointer;
}
.main-nav > ul {
  padding: 0 22px;
}
.main-nav > ul > li > ul {
  display: none;
}
*,
*:before,
*:after {
  box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
<nav class="main-nav">
  <ul>
    <li>Global
      <ul>
        <li>Typography</li>
        <li>Colors</li>
        <li>Icons</li>
      </ul>
    </li>
    <li>Elements
      <ul>
        <li>Text</li>
        <li>Buttons</li>
        <li>Lists</li>
        <li>Tables</li>
        <li>Media</li>
      </ul>
    </li>
    <li>Controls
      <ul>
        <li>dropdown</li>
        <li>alerts</li>
        <li>badges</li>
        <li>modals</li>
      </ul>
    </li>
    <li>Layout
      <ul>
        <li>dynamic row</li>
        <li>flex</li>
      </ul>
    </li>
    <li>Components
      <ul>
        <li>cards</li>
        <li>banners</li>
        <li>itemEditor</li>
        <li>itemIndex</li>
        <li>jQueryUI</li>
        <li>login</li>
        <li>main</li>
        <li>details (and detail views)</li>
        <li>drilldown</li>
        <li>mega menu</li>
        <li>navigation</li>
        <li>search</li>
        <li>thick items</li>
        <li>widgets</li>
      </ul>
    </li>
  </ul>
</nav>