www.webdeveloper.com
Results 1 to 5 of 5

Thread: submenu stay open on menu click

  1. #1
    Join Date
    Mar 2007
    Posts
    44

    submenu stay open on menu click

    Ok, first let me explain what I try to do.

    I have a menu with some items containing a submenu. The submenu's should open when a parent is clicked and contains submenu's, and when traveling to another page (from the item clicked, for example a parent of submenu item), the submenu should remain active and visible.

    When I click on a parent (at the moment the hrefs contain no links just #), the submenu opens. But when I click another main item, the submenu of the previous parent remain visible, and the submenu of the parent just clicked is also visible, while I only want the submenu of the parent clicked to be visible or when parent with no submenu the submenu should be invisible.

    So, here is the code I have so far:
    Code:
    <div id="topnav">
            <ul>
                <li>
                    <a href="index.html">Home</a>
                </li>
                <li>
                    <a href="#">Over Meves</a>
                    <ul class="submenu">
                        <li><a href="#" class="suba">Historie</a></li>
                        <li><a href="#" class="suba">Onze mensen</a></li>
                        <li><a href="#" class="suba">Werkzijze</a></li>
                    </ul>
                </li>
                <li>
                    <a href="vervolg3.html">Disciplines</a>
                    <ul class="submenu">
                        <li><a href="vervolg.html" class="suba">Klimaatbeheersing</a></li>
                        <li><a href="#" class="suba">Elektrotechniek</a></li>
                        <li><a href="#" class="suba">Sanitaire techniek</a></li>
                        <li><a href="#" class="suba">Energiebesparingstechniek</a></li>
                        <li><a href="#" class="suba">Bouwfysica en geluid</a></li>
                        <li><a href="#" class="suba">Diensten energiebesparing</a></li>
                    </ul>
                </li>
                <li>
                    <a href="#">Expertise</a>
                    <ul class="submenu">
                        <li><a href="#" class="suba">Woningbouw & Utiliteit</a></li> 
                        <li><a href="#" class="suba">Zorg & Welzijn</a></li> 
                        <li><a href="#" class="suba">Milieu & Energie</a></li> 
                        <li><a href="#" class="suba">Beheer & Onderhoud</a></li> 
                        <li><a href="#" class="suba">EPA & EPC</a></li> 
                        <li><a href="#" class="suba">Legionella beheersing</a></li>
                    </ul>          	
                </li>
                <li>
                    <a href="#">Contact</a>
                    <ul class="submenu">
                        <li><a href="#" class="suba">Adres & route</a></li> 
                        <li><a href="#" class="suba">Werken bij</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    The javascript:
    Code:
    <script type="text/javascript">
    var ddmenuitem = 0;
    function jsddm_open()
    {  jsddm_close();
       ddmenuitem = $(this).find('ul.submenu').css('display', 'block');
    }
    function jsddm_close()
    {  
    	if(ddmenuitem) ddmenuitem.css('display', 'none');
    }
    $(document).ready(function()
    {  
       $('#topnav > ul > li').bind('click', jsddm_open)
       $('#topnav ul li a.suba').click(function(e){
         if ($(this).attr('class') != 'active'){
           $('#topnav ul li a.suba').removeClass('active');
           $(this).addClass('active');
         }
       });
    	   $("ul.submenu > li > a").each(function () {
             var currentURL = document.location.href;
             var thisURL = $(this).attr("href");
             if (currentURL.indexOf(thisURL) != -1) {
    	         $(this).parents("ul.submenu").css('display', 'block');
             }
           });
    	   $('a').each(function () {
    		var currentURL = document.location.href;
    		var thisURL = $(this).attr('href');
    		if (currentURL = thisURL) {
    			$(this).parents('ul.submenu').css('display', 'block');	
    		}
    	//	else {
    		//	$(this).parents('ul.submenu').css('display', 'none');	
    	//	}
    	   }); 
    });
    </script>
    And the css:
    Code:
    #topnav ul
    {
    	list-style: none;
    	padding: 0;
    	margin: 0;
    }
    #topnav ul li 
    {
    	float: left;
    	margin: 0;
    	padding: 0;
    }
    #topnav ul li a
    {
    	padding: 5px 15px;
    	color: #00537F;
    	text-decoration: none;
    	display: block;
    	font-weight: bold;
    }
    #topnav ul li a:link
    {
    	color: #FFF;
    	text-decoration: none;
    }
    #topnav ul li a:visited
    {
    	color: #FFF;
    	text-decoration: none;
    }
    #topnav ul li a:hover
    {
    	color: #FFF;
    	text-decoration: underline;
    }
    #topnav ul li a.active
    {
    	text-decoration: underline;
    	color: #FFF;
    }
    /*#topnav ul li:hover .submenu
    {
    	display:block;
    }*/
    #topnav ul li ul.submenu
    {
    	float: left;
    	padding: 4px 0;
    	position: absolute;
    	left: 0;
    	top: 24px;
    	display: none;
    	background: #e0e0e0;
    	color: #00537F;
    }
    #topnav ul li ul.submenu a
    {
    	display: inline;
    	color: #00537F;
    	padding: 4px 8px;
    }
    #topnav ul li ul.submenu li
    {
    	border-right-width: 1px; 
    	border-right-style: solid;
    	border-right-color: #00537F;
    }
    #topnav ul li ul.submenu li:last-child
    {
    	border-right-style: none;
    }
    #topnav ul li ul.submenu a:link
    {
    	color: #00537F;
    }
    #topnav ul li ul.submenu a:visited
    {
    	color: #00537F;
    }
    #topnav ul li ul.submenu a:hover
    {
    	text-decoration: underline;
    	color: #00537F;
    }
    #topnav ul li ul.submenu li.active
    {
    	text-decoration: underline;
    	color: #00537F;
    }
    #topnav ul li ul.submenu a.active
    {
    	text-decoration: underline;
    	color: #00537F;
    }
    Hope I explained it clear.

    Thanks for any help.

  2. #2
    Join Date
    Mar 2007
    Posts
    44
    I've made some changes to the jquery code:
    Code:
    var ddmenuitem = 0;
    function jsddm_open()
    {  jsddm_close();
       ddmenuitem = $(this).find('ul.submenu').css('display', 'block');
    }
    function jsddm_close()
    {  
    	if(ddmenuitem) ddmenuitem.css('display', 'none');
    }
    $(document).ready(function()
    {  
       $('#topnav > ul > li').bind('click', jsddm_open)
       $('#topnav ul li a.suba').click(function(e){
         if ($(this).attr('class') != 'active'){
           $('#topnav ul li a.suba').removeClass('active');
           $(this).addClass('active');
         }
       });
    	   $('a').filter(function(){
    			return this.href === document.location.href;
    	   }).addClass('active')
    	   $("ul.submenu > li > a").each(function () {
             var currentURL = document.location.href;
             var thisURL = $(this).attr("href");
             if (currentURL.indexOf(thisURL) != -1) {
    	         $(this).parent("ul.submenu").css('display', 'block');
             }
           });
    });
    Now when I click on a parent item the submenu quickly blinks but is closed even as fast as it opens. I want the submenu to remain open when clicked on a parent that has a submenu, so users can easily navigate.

    Please help.

  3. #3
    Join Date
    Mar 2007
    Posts
    44
    Is there no one who can help me with this problem?

    The only thing that needs to be done is, when click on a parent menu item (going to the href), to script should check/compare the href and check if the parent menu item haschildren, if yes, show submenu.

    I have tried a couple of things but couldn't find a way of doing this.

  4. #4
    Join Date
    Mar 2007
    Posts
    44
    The submenu isn't showing when I load a parent page.

    I've been playing with a code, but this is still not working:
    Code:
    $('#topnav a').each(function(){
      var myHref= $(this).attr('href');
      if( url.match( myHref)) {
    	$(this).addClass('active');
    	$(this).closest('ul').css('display', 'block');
      }
    });
    Can anyone take a look at it?

  5. #5
    Join Date
    Mar 2007
    Posts
    44
    Ok, I solved it with the following jquery code:
    Code:
    $(document).ready(function()
    {  
       $('#topnav ul li ul.submenu li a').click(function(e){
         if ($(this).attr('class') != 'active'){
           $('#topnav ul li a').removeClass('active');
           $(this).addClass('active');
         }
       });
    	   $('a').filter(function(){
    			return this.href === document.location.href;
    	   }).addClass('active')
    	   $("ul.submenu > li > a").each(function () {
             var currentURL = document.location.href;
             var thisURL = $(this).attr("href");
             if (currentURL.indexOf(thisURL) != -1) {
    	         $(this).parents("ul.submenu").css('display', 'block');
             }
           });
           $('#topnav > ul > li > a').each(function(){
          var currURL = document.location.href;
    	  var myHref= $(this).attr('href');
          if (currURL.match(myHref)) {
    			$(this).addClass('active');
    			$(this).parent().find("ul.submenu").css('display', 'block');
    	  }
    	});
    });

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.13438 seconds
  • Memory Usage 2,897KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (6)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates