我有一个带有几个 Accordion 风格项目的导航菜单。一些“父”链接比其他链接拥有更多的 child 。我想改变slideToggle()的速度,以便 child 多的人花更长的时间来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>

最佳答案

建议不要基于高度,而要基于li中有多少个ul元素(如果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>

10-04 22:18