单击下拉菜单外部时关闭下拉菜单

问题描述 投票:0回答:3

我的网站上的导航下拉菜单出现了问题,我几乎已经解决了,但无法完全修复。我担心我的代码可能搞砸了。

当我引入带有 PreventDefault 事件的“滚动到锚标记”功能时,它破坏了我的导航菜单。除非您再次单击菜单按钮,否则菜单不会关闭。在您单击其中一个链接后,我终于将其关闭,但这是现在关闭它的唯一方法。单击菜单按钮或网站上其他任何位置时如何关闭它?我确信 jQuery 的部分是罪魁祸首,但不知道如何修复它或解决它。

菜单 HTML:

  <div class="main-nav navbtn">
    <div class="dropdown"><i onclick="myFunction()" class="dropbtn material-icons md-48">&#xE5D2;</i>
      <div id="myDropdown" class="dropdown-content">
        <a href="#home" class="home navlink">Home</a>
        <a href="#about" class="navlink">About</a>
        <a href="#services" class="navlink">Services</a>
        <a href="#work" class="navlink">Work</a>
        <a href="#testimonials" class="navlink">Testimonials</a>
        <a href="#contact" class="navlink">Contact</a>
        <a href="http://blog.ignitiondesignpdx.com" target="_blank" class="external navlink">Blog</a>
      </div>
    </div>
  </div>

以及相关的 jQuery:

// When the user clicks on the button, toggle between hiding and showing the dropdown content
function myFunction() {
 document.getElementById("myDropdown").classList.toggle("show");
}

//// Close the dropdown menu if the user clicks outside of it
window.onclick = function (event) {
  if (!event.target.matches('.dropbtn')) {
    dropdowns.forEach(function (openDropdown) {
      dropdown.classList.contains('show') && dropdown.classList.remove('show');
    });
   }
 };

////This is the section I made for it to close after clicking a link
jQuery(document).ready(function ($) {
  $('.dropbtn').on('click', function () {
    $(".dropdown-content").show();
  });
  $('.navlink').on('click', function () {
    $(".dropdown-content").hide();
  });
});

这是搞砸其他功能的潜在问题。

//Scroll to anchor tags
$(document).on('click', 'a:not(.external)', function (event) {
  event.preventDefault();

  $('html, body').animate({
    scrollTop: $($.attr(this, 'href')).offset().top
  }, 500);
});

var $root = $('html, body');
$('a').click(function () {
  $root.animate({
    scrollTop: $($.attr(this, 'href')).offset().top
  }, 500);
  return false;
});

我到底应该做什么来修复我的菜单?

您可以查看正在进行的网站 http://idpdx.kreigd.com

更新:我想我已经找到了事情变得混乱的地方。我用来添加下拉功能的函数要求在单击 .dropbtn 元素后添加类“show”,并在再次单击时将其删除。

所以我真正需要做的是修改代码,以便单击 .dropbtn 打开下拉列表,然后单击其他任何内容(包括导航按钮和 .dropbtn 元素)将关闭它。

更新2:尝试不同的方法。忽略第一个更新。

javascript jquery html drop-down-menu menu
3个回答
0
投票

试试这个并让我知道

$(document).click(function() {
    $(".dropdown-content").hide();
    // $(".dropdown-content").removeClass("show");
});

$(".dropdown-content").click(function(e) {
    e.stopPropagation(); // if you click on the div itself it will cancel the click event.
});

0
投票

你可以尝试这样的事情

$('body').not("#myDropdown").off('click').on('click',function(){$("#myDropdown").removeClass("show")});

我尝试了您网站中的代码,但您在 window.click 上编写了一些代码,这导致了问题。


-1
投票

@Nikhil 的回答让我更进一步,但也有缺点。我想出了以下解决方案:

    $(document).click(function (e) {
        var target = $(e.target);
        // check the actual element being clicked is not the dropdown trigger itself
        if (!target.hasClass('dropdown-trigger') && !target.closest('.dropdown-trigger').length) {
            // use the framework to close the dropdown instead of just hiding it: hiding it will require you to click the trigger 2 times to reopen!
            $('dropdown-trigger').dropdown('close');
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.