d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Having Trouble With Multi Level Dropdown Menu
Add Reply New Topic New Poll
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
Jan 13 2015 01:28pm
Hey folks, so I ripped a basic dropdown menu template from javascript-array.com and I'm trying to get it to do a dropdown menu within a dropdown menu, and I've messed around with it and I'm lost.

Here's all the code.

Javascript:

Quote (<head>)

<link type="text/css" href="style.css" rel="stylesheet" />
<script>
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

// open hidden layer
function mopen(id)
{
// cancel close timer
mcancelclosetime();

// close old layer
if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

// get new layer and show it
ddmenuitem = document.getElementById(id);
ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
if(closetimer)
{
  window.clearTimeout(closetimer);
  closetimer = null;
}
}

// close layer when click-out
document.onclick = mclose;

</script>


The HTML:

Quote (<body>)
<ul id="sddm" class="mlddm">
    <li>
    <a href="#"
        onmouseover="mopen('m1')"
        onmouseout="mclosetime()">
          Timeframe [Dropdown Menu]
        </a>
        <div id="m1"
            onmouseover="mcancelclosetime()"
            onmouseout="mclosetime()">
          <a href="#"
          onmouseover="mopen('m1')"
          onmouseout="mclosetime()">
            2015
          </a>
          <div id="m1"
              onmouseover="mcancelclosetime()"
              onmouseout="mclosetime()">
              <a href="#">
            January
            </a>
            </div>
          <a href="#">
          2016
          </a>
          <a href="#">
          2017
          </a>
        </div>
    </li>
</ul>


And the CSS in a different file if it matters:

Quote (style.css)
#sddm
{ margin: 0;
padding: 0;
z-index: 30
}

#sddm li
{ margin: 0;
padding: 0;
list-style: none;
float: left;
font: bold 11px arial
}

#sddm li a
{ display: block;
margin: 0 1px 0 0;
padding: 4px 10px;
width: 60px;
background: #5970B2;
color: #FFF;
text-align: center;
text-decoration: none
}

#sddm li a:hover
{ background: #49A3FF
}

#sddm div
{ position: absolute;
visibility: hidden;
margin: 0;
padding: 0;
background: #EAEBD8;
border: 1px solid #5970B2
}

#sddm div a
{ position: relative;
display: block;
margin: 0;
padding: 5px 10px;
width: auto;
white-space: nowrap;
text-align: left;
text-decoration: none;
background: #EAEBD8;
color: #2875DE;
font: 11px arial
}

#sddm div a:hover
{ background: #49A3FF;
color: #FFF
}


Basically, I'm trying to have a menu that has this kind of tier system:

2015
---January
--------1
--------2
--------3
--------etc...
---February
---March
---etc...
2016
2017
etc...

So far, the first tier drops down (in other words, when I hover over Timeframe [Dropdown Menu], I see 2015, 2016, and 2017). But when I try and hover over 2015, January doesn't pop up.

Anyone notice any glaring errors or have any input? Thanks!

Member
Posts: 82
Joined: Nov 11 2014
Gold: 200.00
Jan 15 2015 04:03pm
While you should be able to do it with div tags, it is far easier and more manageable in my opinion to use nested ul and li tags.

Refer to this: http://javascript-array.com/scripts/multi_level_drop_down_menu/

So, with respect to that tier system you mentioned, it would look something like below and use CSS and JS to show/hide tiers as necessary.

Code
<ul>
<li>
2015
<ul>
<li>
January
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
<li>February</li>
<li>March</li>
</ul>
</li>
<li>
2016
</li>
<li>
2017
</li>
</ul>


For specifics, refer to: http://www.sanwebe.com/2013/06/multi-level-dropdown-menu-css

That example uses jQuery to work, but since jQuery is nothing more than a Javascript library, if you wanted to code it from scratch, all you'd need to do is see how jQuery implements hover, slides, and hide functions.

This post was edited by SasorizaKR on Jan 15 2015 04:08pm
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
Jan 15 2015 07:33pm
Oh yeah that's much easier lol. Thank you for that.

Now to use creative googling to figure out how to make sub-options pop up at the top of the menu instead of by whatever option they came from (if that makes sense).
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll