在其外部点击时关闭下拉菜单

时间:2016-11-09 17:04:30

标签: javascript jquery html drop-down-menu menu

我的网站上的导航下拉列表出现了问题,我几乎已经解决但无法解决问题。我担心我的代码可能弄得一团糟。

当我使用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:尝试不同的方法。忽略第一次更新。

3 个答案:

答案 0 :(得分:0)

试试这个&amp;让我知道

$(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.
});

答案 1 :(得分:0)

你可以试试这样的东西

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

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

答案 2 :(得分:0)

@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');
        }
    });