Note that there are some explanatory texts on larger screens.

plurals
  1. POResponsive JavaScript drop down menu problems
    primarykey
    data
    text
    <p>Based on <a href="http://webdesign.tutsplus.com/tutorials/site-elements/big-menus-small-screens-responsive-multi-level-navigation/" rel="nofollow">this tutorial over at Webdesigntuts+</a>, I am implementing a responsive JavaScript menu which supports sub menus / drop downs.</p> <p>Unfortunately, until the orientation of the device is changed, or the browser width is altered, the sub menu / drop down system doesn't work.</p> <p>Here's the JavaScript / jQuery code:</p> <pre><code>var ww = $(window).width(); $(document).ready(function() { adjustMenu(); $(".nav li a").each(function() { if ($(this).next().length &gt; 0) { $(this).addClass("parent"); }; }) $(".toggle-menu").click(function(e) { e.preventDefault(); $(this).toggleClass("active"); $(".nav").toggle(); }); }); $(window).bind('resize orientationchange', function() { ww = $(window).width(); adjustMenu(); }); var adjustMenu = function() { if (ww &lt;= 480) { $(".toggle-menu").css("display", "inline-block"); if (!$(".toggle-menu").hasClass("active")) { $(".nav").hide(); } else { $(".nav").show(); } $(".nav li").unbind('mouseenter mouseleave'); $(".nav li a.parent").unbind('click').bind('click', function(e) { // must be attached to anchor element to prevent bubbling e.preventDefault(); $(this).parent("li").toggleClass("hover"); }); } else if (ww &gt; 480) { $(".toggle-menu").css("display", "none"); $(".nav").show(); $(".nav li").removeClass("hover"); $(".nav li a").unbind('click'); $(".nav li").unbind('mouseenter mouseleave').bind('mouseenter mouseleave', function() { // must be attached to li so that mouseleave is not triggered when hover over submenu $(this).toggleClass('hover'); }); } } </code></pre> <p>Here is some sample HTML: </p> <pre><code> &lt;a class="toggle-menu" href="#"&gt;&lt;img src="/images/responsive/menu.png" alt="Menu"&gt;&lt;/a&gt; &lt;ul class="nav"&gt; &lt;li&gt;&lt;a href="/"&gt;Home&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="/about/"&gt;About Us&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="/url/"&gt;Menu item&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="/url/"&gt;Menu item intro&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="/url/sub1/"&gt;Submenu item 1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="/url/sub2/"&gt;Submenu item 2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="/url/sub3/"&gt;Submenu item 3&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p>The navigation menu is hidden to begin with until the .toggle-menu link is clicked on. Also, the sub navigation is hidden until the parent link is clicked on.</p> <p>Could anyone with more experience of jQuery / JavaScript suggest any potential solutions please?</p> <p>Many thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload