【问题标题】:jQuery - Resetting the height of an element not workingjQuery - 重置元素的高度不起作用
【发布时间】:2019-06-28 15:07:37
【问题描述】:

我想要做的是从画布菜单中获取下拉菜单的高度并将其设置为 UL,以便它覆盖菜单当前所在位置的高度。所以如果下拉菜单的高度很长,它所在的 div 应该展开并且边框底部应该始终可见。

如果您单击 Corporate 几次,然后单击 SME,然后返回 Corporate,则 Corporate 下拉菜单位于 DIV 的顶部,并且高度不会被重置。我在解释东西时很糟糕,所以看看这里的网站:Site URL

这里是 jQuery

// SLIDE OUT MENU
  $(".menu-blue-bar button").on("click", function(e) {
    $(".menu-blue-bar").toggleClass("menu-slide");
    $(".menu-blue-bar button").toggleClass("is-active");
    $(".menu-blue-bar .fade-in-menu .row").toggleClass("in-view");
    e.preventDefault();
  });

  jQuery(
    "<li class='back-button'><i class='icon icon-carousel-left-arrow'></i></li>"
  ).insertBefore(
    ".menu-blue-bar .main-menu-links .dropdown-menu li:first-child"
  );

  jQuery("li.back-button").on("click", function() {
    jQuery(".menu-blue-bar ul.dropdown-menu").removeClass("show");
    jQuery("ul.navbar-nav").css("height", "");
    console.log("clicked back arrow");
  });

  jQuery(".menu-blue-bar .navbar-nav > li.dropdown").on("click", function() {
    if (!$(".menu-blue-bar ul.dropdown-menu").hasClass("show")) {
      $(".main-menu-links ul.navbar-nav").css({
        height:
          $(this)
            .find("ul.dropdown-menu")
            .height() + "px"
      });
    }
  });

截图:screenshot link

【问题讨论】:

    标签: javascript jquery css wordpress off-canvas-menu


    【解决方案1】:

    当您“重置”height 时,您没有提供值

    jQuery("ul.navbar-nav").css("height", "");
    

    在 CSS 中不是有效的单位大小。

    改为尝试将高度设置为 0 或 initial

    jQuery("ul.navbar-nav").css("height", "initial");
    

    height 这个词也应该是一个字符串(添加引号)。 这个

    $(".main-menu-links ul.navbar-nav").css({
            height:
              $(this)
                .find("ul.dropdown-menu")
                .height() + "px"
          });
    

    应该是

    $(".main-menu-links ul.navbar-nav").css({
            "height":
              $(this)
                .find("ul.dropdown-menu")
                .height() + "px"
          });
    

    可能还有更多,但从那开始。

    【讨论】:

    • 谢谢 Bryce 会在周末或周一早上看一下。将报告我的进展情况。 ?
    • 没有产生积极影响,将不得不进一步调查,看看什么不起作用。
    猜你喜欢
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多