【问题标题】:I'm trying to collapse and close the nav element with jQuery我正在尝试使用 jQuery 折叠和关闭导航元素
【发布时间】:2019-04-16 12:31:48
【问题描述】:

我正在尝试使用 jQuery 折叠和关闭导航元素。我正在使用带有 gem coffee-rails 和 4.2.2 gem jquery-rails, 4.3.1 的 rails 5,但 animate 选项没有响应。

$(document).ready(function() {
  $('nav a.mobile_menu').on('click', function() {
    var currentNavHeight = $('nav').height();
    if (currentNavHeight < 6) {
      var newNavHeight = $('nav > ul').height() + 15;
      $('nav').animate({
        'height': newNavHeight + 'px'
      }, 750);
    } else {
      $('nav').animate({
        'height': '0px'
      }, 750);
    }
  });
});
nav {
  padding: 50px 15px 20px 15px;
  background-color: #4b0a0c;
  height: 0px;
  overflow: hidden;
}

【问题讨论】:

    标签: jquery css ruby-on-rails


    【解决方案1】:

    【讨论】:

    • 请不要使用 W3Schools 作为参考。他们的指南经常过时,有时甚至完全是错误的。处理 jQuery 时,请使用他们的参考:api.jquery.com。此外,OP 可能没有使用 toggle(),因为它不会像他们的逻辑尝试那样为元素设置动画。 slideToggle() 会更合适
    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 2018-06-04
    • 2023-03-08
    • 2020-09-15
    • 1970-01-01
    • 2015-08-03
    • 2015-04-12
    • 2015-06-11
    相关资源
    最近更新 更多