Search code examples
javascripthtmlcssmenu

Collapsed menu, height issue


I'm currently setting up a menu including sub menu, built on Wordpress categories. Basically, I'm retrieving all the top level categories, and then build a submenu on each, with all the posts from that parent category.

So the structure looks like this :

<ul class="menuCat">
   <li> <a href="#" title="lifestyle">lifestyle</a>
      <ul class="show-hide">
         <li><a href="http://localhost/wordpress/article-7/">Article #7</a></li>
         <li><a href="http://localhost/wordpress/article-5/">Article #5</a></li>
         <li><a href="http://localhost/wordpress/hello-world/">Article #3</a></li>
      </ul>
   </li>
   <li> <a href="#" title="musique">musique</a>
      <ul class="show-hide">
         <li><a href="http://localhost/wordpress/article-8/">Article #8</a></li>
         <li><a href="http://localhost/wordpress/article-7/">Article #7</a></li>
         <li><a href="http://localhost/wordpress/article-2/">Article #2</a></li>
         <li><a href="http://localhost/wordpress/article-1/">Article #1</a></li>
         <li><a href="http://localhost/wordpress/hello-world/">Article #3</a></li>
      </ul>
   </li>
</ul>
<div id="content">...

The sub menus are set to display:none. When a category gets clicked, the submenu ul appears (using jQuery toggle) under the main menu. I'm running it locally so I can't give you a live example, but the way it works is the same as when you hit the "categories" link here : http://wpshower.com/demo/?theme=imbalance.

My problem is that with this structure and for what I want to visually achieve (c.f previous url), I don't see any other option that putting the submenu block in an absolute position. But if I do so, I need to push the rest of the content down, when a menu is triggered. What I've tried so far, is to set a margin-top based on the height of the currently viewed submenu. Unfortunately, neither height or outerHeight could help me...

Any ideas ?

Thanks!


Solution

  • Can you go into more detail about why you need to position absolute? Looks to me like you could achieve what you want by having all your submenus statically positioned below the top level menu, using jQuery to ensure only one is shown at a time.

    By having them statically positioned the submenu will push the content below it down when it expands, and as long as all but one submenu is always set to display: none; you won't even know it's there.

    For this to work however, you'll need to change the structure of your html though. The submenu items will need to be in a div below the top level menu list, not within it.

    <ul class="menuCat">
       <li> <a href="#" title="lifestyle">lifestyle</a>
       </li>
       <li> <a href="#" title="musique">musique</a>
       </li>
    </ul>
    <div id="submenu-container">
          <ul class="show-hide lifestyle">
             <li><a href="http://localhost/wordpress/article-7/">Article #7</a></li>
             <li><a href="http://localhost/wordpress/article-5/">Article #5</a></li>
             <li><a href="http://localhost/wordpress/hello-world/">Article #3</a></li>
          </ul>
          <ul class="show-hide musique">
             <li><a href="http://localhost/wordpress/article-8/">Article #8</a></li>
             <li><a href="http://localhost/wordpress/article-7/">Article #7</a></li>
             <li><a href="http://localhost/wordpress/article-2/">Article #2</a></li>
             <li><a href="http://localhost/wordpress/article-1/">Article #1</a></li>
             <li><a href="http://localhost/wordpress/hello-world/">Article #3</a></li>
          </ul>
    </div>
    <div id="content"></div>
    
    $(function () {
        $('.menuCat > li').click(function (e) {
            var target = $('.show-hide.' + e.target.title);
            $('.show-hide').not(target).hide();
            target.toggle();
        });
    });
    
    
    
    ul.menuCat li {
        display:inline-block;
        vertical-align: top;
    }
    ul.show-hide {
        display: none;
        background-color:lightgrey;
    }
    #content {
        height: 200px;
        width: 100%;
        background-color: red;
    }
    

    For an example see this demo: http://jsfiddle.net/ijoukov/PCgxR/