我正在尝试使用jQuery折叠并关闭nav元素。我正在将Rails 5与gem coffee-rails和4.2.2 gem jquery-rails(4.3.1)一起使用,但是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的toggle()方法?

https://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_eff_toggle

https://api.jquery.com/toggle/

关于jquery - 我正在尝试使用jQuery折叠并关闭nav元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53285000/

10-15 13:48