3

I'm trying to create a slick fade in/fade out effect when users hover over an unordered list menu by using jQuery.Thus far I can fade in the sub menu when users hover over a menu item containing sub items but I'm struggling to hide it.

The requirement is as follows - If the mouse is not over the sub menu items or it's parent - fade out the sub menu list.

I'm including a screen shot of my menu and the HTML markup, please suggest how can i hide the menu as per the requirement listed above:

menu

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Home</title>
    <link href="Styles/Style.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        jQuery(document).ready(function () {

            $('#subList').css("display", "none");

            $('#item3').mouseover(function () {
                $('#subList').fadeIn('slow');
                $('#subList').css("display", "block");
            });

            $('#subItem1').mouseover(function () {
                $('#subList').css("display", "block");
            });

            $('#subItem2').mouseover(function () {
                $('#subList').css("display", "block");
            });

            $("#subItem1").mouseleave(function () {
                $("#subList").fadeOut("slow");
            });

            $('#subItem2').mouseleave(function () {
                $('#subList').fadeOut("slow");
            });
        });
    </script>
</head>
<body>
    <div class="menu">
        <ul>
            <li><a href="item4.htm">Menu Item 4</a></li>
            <li><a href="#" id="item3">Menu Item3</a>
                <ul id="subList">
                    <li id="subItem1"><a href="subItem1.htm">Sub Item 1</a></li>
                    <li id="subItem2"><a href="subItem2.htm">Sub Item 2</a></li>
                </ul>
            </li>
            <li><a href="item2.htm">Menu Item 2</a></li>
            <li><a href="item1.htm">Menu Item 1</a></li>
        </ul>
    </div>
</body>
</html>
4

3 回答 3

7

试试这个脚本:

$(function(){
    $(".menu ul li").hover(function(){
             $(this).children("ul").stop().fadeIn("slow");
       },
       function(){
             $(this).children("ul").stop().fadeOut("slow");   
    })
})

http://jsfiddle.net/2yEtK/1/

于 2012-04-25T05:42:53.070 回答
1

试试这个脚本:

 jQuery(document).ready(function () {

        $('#subList').css("display", "none");

        $('#item3').parent().hover(function () {
            $('#subList').fadeIn('slow');
            $('#subList').show();
        },
            function(){
                $('#subList').fadeOut('slow');
                $('#subList').hide();
        }
        );
    });

这里有 JSFiddle 链接:http: //jsfiddle.net/scQ9W/1/

您还可以使用 slideDown() 和 slideUp()。请参阅此链接:http: //jsfiddle.net/scQ9W/2/

应用一些css后:http: //jsfiddle.net/scQ9W/3/

于 2012-04-25T05:43:05.933 回答
0

跳过 $("#subItem1/subItem2").mouseleave 并使用...

$('#subList').mouseleave(function () {
   $(this).fadeOut("slow");
});

反而?

于 2012-04-25T05:46:32.703 回答