简体   繁体   English

移动下拉菜单不会关闭JavaScript

[英]Mobile dropdown menu wont close javascript

I am trying to have a hamburger menu on my mobile website. 我正在尝试在移动网站上使用汉堡包菜单。

When viewed on a mobile device, The menu opens on a click but will not collapse on a click once the menu is open. 在移动设备上查看时,该菜单会在单击时打开,但在打开菜单后不会在单击时折叠。 -THIS IS MY ISSUE -这是我的问题

This is basic HTML for my issue. 这是我的问题的基本HTML。

I am using magento 1.9.2.3 我正在使用magento 1.9.2.3

The php code are the "toplinks". php代码是“ toplinks”。 These do not need to be working to solve this problem. 这些不需要解决这个问题。

HTML HTML

<div class="mobile-links">

        <div id="dropdown" onClick="myFunction()">
        <button class="dropbtn">
        <span><img src="http://www.smockedoverstocks.net/skin/frontend/lee/default/images/hamburger-menu.png"/></span></button>
        <div class="dropdown-content">
        <div class="quick-access five columns omega">
        <?php echo $this->getChildHtml('topLinks') ?>
        </div>
        </div>
        </div>
</div>

Javascript that controls the menu: 控制菜单的Javascript:

        <script language="javascript">
 /* When the user clicks on the button, 
 toggle between hiding and showing the dropdown content */
 function myFunction(ev) {
document.getElementById("dropdown").classList.toggle(show);
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {

var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
  var openDropdown = dropdowns[i];
  if (openDropdown.classList.contains('show')) {
  Dropdown.classList.remove('show')};

  }
}
}
}
</script>

CSS for the menu: 菜单的CSS:

.mobile-links .dropdown {
  position:relative;
  display:inline-block;
  padding:2px 13px 0px;
}

.mobile-links .dropdown-content{
  display:none;
  position:absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  padding: 5px 12px;
  z-index: 1;
}

#dropdown:hover .dropdown-content,
#dropdown.show .dropdown-content {
display:block;
}

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

First, 第一,

   <div class="dropdown">
      <onclick="myFunction()" class="dropbtn">

is incorrect format. 格式不正确。 onClick should be inline with your <div> like so, onClick应该与您的<div>内联,

 <div class="dropdown" onClick="myFunction()">

or on a different user interface element. 或在其他用户界面元素上。 Next, 下一个,

document.getElementById("myDropdown").classList.toggle("show");

should be changed to, 应该改为

document.getElementById("myDropdown").classList.toggle();

Set the initial state of myDropdown to hide() . myDropdown的初始状态设置为hide() For example, 例如,

  $(document).ready(function() {
      $('#myDropdown').hide();
  }

This specifies an element to perform an action after the page has loaded. 这指定页面加载后执行操作的元素。 You could set it entirely with css by giving a display property of none and setting a trigger to change the display property to block on click or hover . 您可以使用css完全设置它,方法是none设置display属性,并设置触发器以更改display属性以在clickhover block Hope this helps 希望这可以帮助

Have at this adapted live example: Is this the behaviour you were looking to achieve? 在这个经过修改的实时示例中:这是您想要实现的行为吗?

 /* When the user clicks on the button, toggle between hiding and showing the dropdown content */ function myFunction(ev) { document.getElementById("dropdown").classList.toggle('show'); } // Close the dropdown menu if the user clicks outside of it window.onclick = function(event) { if (!event.target.matches('.dropbtn')) { var dropdowns = document.getElementsByClassName("dropdown-content"); var i; for (i = 0; i < dropdowns.length; i++) { var openDropdown = dropdowns[i]; if (openDropdown.classList.contains('show')) { openDropdown.classList.collapse('show'); } } } } 
 .mobile-links .dropdown { position:relative; display:inline-block; padding:2px 13px 0px; } .mobile-links .dropdown-content{ display:none; position:absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); padding: 5px 12px; z-index: 1; } #dropdown:hover .dropdown-content, #dropdown.show .dropdown-content { display:block; } 
 <div class="mobile-links"> <div id="dropdown" onclick="myFunction()"> <button class="dropbtn"> <span><img src="http://www.smockedoverstocks.net/skin/frontend/lee/default/images/hamburger-menu.png"/></span></button> <div class="dropdown-content"> <div class="quick-access five columns omega"> content </div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM