Fade ul menu item insert and exit on mouse

I am trying to create a fade in / fade out slide effect when users hover over an unordered list menu using jQuery. This way, I can disappear in the submenu when users hover over a menu item containing sub-items, but I am trying to hide it.

The requirement is the following: if the mouse is not above the submenu or the parent, the list of submenus disappears.

I include a screenshot of my menu and HTML markup, please suggest how I can hide the menu in accordance with the above requirement:

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>
+3
source share
3 answers

try this script:

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

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

+7
source

Try this Script:

 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/

+1

Skip $ ("# subItem1 / subItem2"). mouseleave and use ...

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

instead

0
source

All Articles