1

I do not know a lot of JS. Although I already did my own search, but I could not find out answers. So I am asking here and hope you can help me out.

I am trying to create a navigation menu based on div tag (like http://www.adobe.com/), and using jQuery to make a function for "appear/disapper when hover".

Simple Div Structure:

<div id='menu'>
    <div> Level 1 a
            <div> Level 2 a </div> 
            <div> Level 2 b </div> 
    </div>
    <div> Level 1 b
            <div> Level 2 c </div> 
            <div> Level 2 d </div> 
    </div>
</div>

I understand that it will need to use $('#menu').hover() function. My question is, if only use one id "menu", how or what kind of function I can use to determine which actual menu list is being hovered??

Like:

$("#menu").hover(                        // Div Menu is being hovered
    function () {
        // $el = Determine which menu inside of Div Menu is actually being hovered
        // $el.show();
    },
    function () {
        $el..hide();
    }
);

Or maybe my structure is completely wrong, Should use another method to do this? Please help.

4

4 回答 4

3
$("#menu").hover(                        // Div Menu is being hovered
    function (event) {
        $el = $(event.target);
        $el.show();
    },
    function (event) {
        $el = $(event.target);
        $el.hide();
    }
);
于 2012-12-21T01:58:01.613 回答
1

Actually, there is no hover event. There are many different mouse events in two different models, and they are different in getting triggered from inner elements. Luckily, jQuery's hover method (actually mouseenter and mouseleave) abstracts over this and fires the handlers only when the parent element is hovered.

This means you have to bind the handler to every single element in the menu tree:

$("#menu div").hover(
    function (event) {
        console.log(event);
        $(this).children().show();
    },
    function (event) {
        $(this).children().hide();
    }
);

Demo at jsfiddle.net

于 2012-12-21T02:04:23.083 回答
1

Yes you could use the code you wrote for determining when you hover a div. Then you trigger a function for displaying the dropdown menu. When you define the css of the navigation bar you should set the part that doesn't have to be visible at the beginning to display:hidden; in the div, so it's hidden. Then through jquery you inject code into the css for changing the property display. I give you an example. Let's assume you create a div called "hidden" and set this in the css among other possible styles: #hidden { display:hidden }

Then you want the part with id "hidden" to appear when you hover the mouse. You can use:

$("#hidden").hover.css('display', 'block')

so the hidden part will appear. Anyway you can create a dropdown menu even simply by using css only without jquery. Here i give you an example: Let's say you have this markup in the html file

<ul id="nav">
<li>
    <a href="#">Home</a>
</li>

<li>
    <a href="#">About</a>
    <ul>
        <li><a href="#">The product</a></li>

        <li><a href="#">Meet the team</a></li>
    </ul>
</li>
<li>
    <a href="#">Services</a>

    <ul>
        <li><a href="#">Sevice one</a></li>
        <li><a href="#">Sevice two</a></li>

        <li><a href="#">Sevice three</a></li>
        <li><a href="#">Sevice four</a></li>
    </ul>

</li>
<li>
    <a href="#">Product</a>
    <ul>
        <li><a href="#">Small product (one)</a></li>

        <li><a href="#">Small product (two)</a></li>
        <li><a href="#">Small product (three)</a></li>
        <li><a href="#">Small product (four)</a></li>

        <li><a href="#">Big product (five)</a></li>
        <li><a href="#">Big product (six)</a></li>
        <li><a href="#">Big product (seven)</a></li>

        <li><a href="#">Big product (eight)</a></li>
        <li><a href="#">Enourmous product (nine)</a></li>
        <li><a href="#">Enourmous product (ten)</a></li>

        <li><a href="#">Enourmous product (eleven)</a></li>
    </ul>
</li>
<li>
    <a href="#">Contact</a>

    <ul>
        <li><a href="#">Out-of-hours</a></li>
        <li><a href="#">Directions</a></li>

    </ul>
</li>
</ul>

As you can see here the markup is simply a series of nested "ul". No verbose IDs/classes, no divs, just rich, semantic code.

The #nav ul contains a series of li, and any that require a dropdown then contain another ul. Notice the dropdown ul have no classes on them—this is because we use the cascade to style these, keeping our markup even cleaner.

Now the CSS:

#nav{
list-style:none;
font-weight:bold;
margin-bottom:10px;
/* Clear floats */
float:left;
width:100%;
/* Bring the nav above everything else--uncomment if needed.
position:relative;
z-index:5;
*/
}
#nav li{
float:left;
margin-right:10px;
position:relative;
}
#nav a{
display:block;
padding:5px;
color:#fff;
background:#333;
text-decoration:none;
}
#nav a:hover{
color:#fff;
background:#6b0c36;
text-decoration:underline;
}

/*--- DROPDOWN ---*/
#nav ul{
background:#fff; /* Adding a background makes the dropdown work properly in IE7+.     Make this as close to your page's background as possible (i.e. white page == white background). */
background:rgba(255,255,255,0); /* But! Let's make the background fully transparent      where we can, we don't actually want to see it if we can help it... */
list-style:none;
position:absolute;
left:-9999px; /* Hide off-screen when not needed (this is more accessible than     display:none;) */
}
#nav ul li{
padding-top:1px; /* Introducing a padding between the li and the a give the   illusion spaced items */
float:none;
}
#nav ul a{
white-space:nowrap; /* Stop text wrapping and creating multi-line dropdown items */
}
#nav li:hover ul{ /* Display the dropdown on hover */
left:0; /* Bring back on-screen when needed */
}
#nav li:hover a{ /* These create persistent hover states, meaning the top-most link stays 'hovered' even when your cursor has moved down the list. */
background:#6b0c36;
text-decoration:underline;
}
#nav li:hover ul a{ /* The persistent hover state does however create a global style for links even before they're hovered. Here we undo these effects. */
text-decoration:none;
}
#nav li:hover ul li a:hover{ /* Here we define the most explicit hover states--what happens when you hover each individual link. */
background:#333;
}

So by using a nested unordered list and some css you can make an effective dropdown menu. That is the best solution according to me. Because the easier way you can make a thing the better it is.

For more details and a full explaination and demo of the dropdown menu, go to: http://csswizardry.com/2011/02/creating-a-pure-css-dropdown-menu/

Of course you can set the colors and style as you prefer.

于 2012-12-21T02:25:41.843 回答
1

If you want a flyout vertical menu like that on Amazon check this example. It's simple, just html and css, no jquery. It looks alike.

HTML:

<ul class="nav">
<li>
    <a href="#">
        <strong>MP3s &amp; Cloud Player</strong> 18 million songs, play anywhere
    </a>
</li>
<li>
    <a href="#">
        <strong>MP3s &amp; Cloud Player</strong> 18 million songs, play anywhere
    </a>
    <ul>
        <li>
            <a href="#">
                <strong>Your Cloud Drive</strong> Anythign digital, securely stored, available anywhere
            </a>
        </li>
        <li>
            <a href="#">
                <strong>Learn more about cloud</strong>                </a>
        </li>
    </ul>
    <span class="cover"></span>
</li>
<li>
    <a href="#">
        <strong>Kindle</strong>
    </a>
</li>
</ul>​

CSS:

ul.nav{
font-size: 10px;
font-family: Verdana, Helvetica;
width: 200px;
background: #edf7ff;      
}

ul.nav li{
padding: 5px 4px;   
border: 1px solid #85abc9;
margin-bottom: -1px;
position: relative;
background: url(http://www.qualitymetric.com/Portals/0/images/orange_arrow.png) no-    repeat 185px center;
}

ul.nav > li:hover{
background: #fff;
border: 1px solid #999;
z-index:1;
box-shadow: 0px 1px 0px #999;
-moz-box-shadow: 0px 1px 0px #999;
}

ul.nav > li:hover > span{
width: 5px;
height: 100%;
background: #fff;
position: absolute;
top: 0px;
bottom: 0px;
right: 15px;
z-index: 10;
}

ul.nav li a{
color: #666;
text-decoration: none;
}

ul.nav li a strong{
font-size: 11px;
color: #333;
font-weight: bold;
display: block;
}

/* dropdown */

ul.nav li ul{
width: 200px;
padding-left: 12px;
background: #fff;
border: 1px solid #999;
position: absolute;
border-radius: 4px;
-moz-border-radius: 4px;
box-shadow: 1px 1px 0px #999;
-moz-box-shadow: 1px 1px 0px #999;
top: -1px;
left: 180px;
z-index: 9;
display: none;
}

ul.nav li:hover > ul{
display: block;           
}

ul.nav li ul li{
border: none;
padding-left: 12px;
background: url(http://www.qualitymetric.com/Portals/0/images/orange_arrow.png) no-    repeat 0px 6px;
}

ul.nav li ul li a strong{
font-weight: normal;
color: #034995;
}

Look at the code and demo here: http://jsfiddle.net/blackpla9ue/KHLgm/8/ You can edit and add things as you prefer. ​</p>

于 2012-12-21T03:25:07.753 回答