简体   繁体   English

打开和关闭移动响应菜单

[英]Opening and closing mobile responsive menu

I'm trying to build a mobile responsive "hamburger" menu. 我正在尝试建立移动响应式“汉堡”菜单。 The html which I'm using for this is below... 我为此使用的html如下...

 .menu_closed { color: red; } .menu_open { color: blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script type="text/javascript"> $('span.menu_closed').click(function() { $('ul.menu').toggle('1000'); $(this).toggleClass("menu_open menu_closed"); }); </script> <span class="menu_closed">&#9776;</span> <ul class="menu" id="menu"> <ul class='section' id='section_1'> <li><span id='section_title_1' class='section_title'><a href='#' id='section_link_1'>Against the odds.</a></span> <ul> <li id='exhibit_1' class='exhibit_title'><a href="../against-the-odds/introduction"> &rarr; Introduction</a> </li> <li id='exhibit_2' class='exhibit_title'><a href='../against-the-odds/deriving-functions'> &rarr; Deriving functions</a> </li> <li id='exhibit_3' class='exhibit_title'><a href='../against-the-odds/exploiting-odds'> &rarr; Exploiting odds</a> </li> <li id='exhibit_4' class='exhibit_title'><a href='../against-the-odds/betting_history'> &rarr; Betting history</a> </li> </ul> </li> </ul> <ul class='section' id='section_2'> <li><span id='section_title_2' class='section_title'><a href='http://themathsproject.co.uk' id='section_link_2'>Remembering everything.</a></span> <ul> <li id='exhibit_104' class='exhibit_title'><a href='#'>black swans</a> </li> </ul> </li> </ul> <ul class='section' id='section_5'> <li><span id='section_title_5' class='section_title'><a href='http://themathsproject.co.uk' id='section_link_5'>Running faster.</a></span> <ul> <li id='exhibit_107' class='exhibit_title'><a href='#'>possible areas to explore</a> </li> <li id='exhibit_108' class='exhibit_title'><a href='#'>developing the model</a> </li> <li id='exhibit_109' class='exhibit_title'><a href='#'>performance</a> </li> </ul> </li> </ul> <ul class='section' id='section_4'> </ul> <div class='bot'> <p><a href='https://www.twitter.com/themathsproject' target="_blank">twitter</a> <br /> <a href='https://www.facebook.com/themathsproject' target="_blank">facebook</a> </p> </div> </ul> 

For some reason, it's not closing and opening as it should. 由于某种原因,它并没有像应该那样关闭和打开。 I've no idea what's wrong. 我不知道怎么了

I would be grateful if anybody could help. 如果有人可以帮助,我将不胜感激。

Jack 插口

$('span.menu_closed').click(function() { should reference a standard menu class, like .hamburger , not .menu_closed because if you change .menu_closed to .menu_open then .menu_closed no longer exists and you'll lose the click handler. $('span.menu_closed').click(function() {应该引用标准菜单类,例如.hamburger ,而不是.menu_closed因为如果将.menu_closed更改为.menu_open.menu_closed不再存在,您将失去点击处理程序。

$(this).toggleClass("menu_open menu_closed"); should just toggle a class like open or closed . 应该只是切换一个openclosed的类。 Technically what you were doing would work OK where the classname was .menu_open and you update this $.toggleClass() line to toggle the class .menu_closed , but then you end up with an element called span.menu_open.menu_closed . 从技术上讲,您正在做的工作在类名称为.menu_open可以正常工作,并且您更新了$.toggleClass()行以切换类.menu_closed ,但是最后得到了一个名为span.menu_open.menu_closed的元素。 Better to name the menu something agnostic, then toggle whether it's open or not. 最好将菜单命名为不可知的,然后切换菜单是否打开。

Also added cursor: pointer; 还添加了cursor: pointer; to the menu to give an indication to the user that you can click the element. 菜单上,向用户指示您可以单击该元素。

Wrapped your jQuery code in $(function() {}); 将您的jQuery代码包装在$(function() {}); which will cause the page to wait to run the JS until the DOM is ready. 这将导致页面等待运行JS,直到DOM准备就绪为止。 This is needed since your JS comes before the rest of the HTML on the page. 这是必需的,因为您的JS位于页面上其余HTML之前。

And changed your click handler from using $.live() (deprecated) to $.on() . 并将点击处理程序从使用$.live() (不推荐使用)更改$.on() $.live() was deprecated in 1.7 and removed in 1.9 , and jQuery advises using $.on() now to attach event handlers. $.live() 在1.7已弃用,并在1.9中删除 ,jQuery建议立即使用$.on()附加事件处理程序。

 .hamburger { color: red; cursor: pointer; } .open { color: blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> $(function() { $('.hamburger').on('click',function() { $('ul.menu').toggle(1000); $(this).toggleClass("open"); }); }); </script> <span class="hamburger">&#9776;</span> <ul class="menu" id="menu"> <ul class='section' id='section_1'> <li><span id='section_title_1' class='section_title'><a href='#' id='section_link_1'>Against the odds.</a></span> <ul> <li id='exhibit_1' class='exhibit_title'><a href="../against-the-odds/introduction"> &rarr; Introduction</a> </li> <li id='exhibit_2' class='exhibit_title'><a href='../against-the-odds/deriving-functions'> &rarr; Deriving functions</a> </li> <li id='exhibit_3' class='exhibit_title'><a href='../against-the-odds/exploiting-odds'> &rarr; Exploiting odds</a> </li> <li id='exhibit_4' class='exhibit_title'><a href='../against-the-odds/betting_history'> &rarr; Betting history</a> </li> </ul> </li> </ul> <ul class='section' id='section_2'> <li><span id='section_title_2' class='section_title'><a href='http://themathsproject.co.uk' id='section_link_2'>Remembering everything.</a></span> <ul> <li id='exhibit_104' class='exhibit_title'><a href='#'>black swans</a> </li> </ul> </li> </ul> <ul class='section' id='section_5'> <li><span id='section_title_5' class='section_title'><a href='http://themathsproject.co.uk' id='section_link_5'>Running faster.</a></span> <ul> <li id='exhibit_107' class='exhibit_title'><a href='#'>possible areas to explore</a> </li> <li id='exhibit_108' class='exhibit_title'><a href='#'>developing the model</a> </li> <li id='exhibit_109' class='exhibit_title'><a href='#'>performance</a> </li> </ul> </li> </ul> <ul class='section' id='section_4'> </ul> <div class='bot'> <p><a href='https://www.twitter.com/themathsproject' target="_blank">twitter</a> <br /> <a href='https://www.facebook.com/themathsproject' target="_blank">facebook</a> </p> </div> </ul> 

hey man i tried your code it's working fine here is 

https://jsfiddle.net/L0vnd4ay/

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

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