简体   繁体   中英

jQuery menu - keep menu active on dropdown hover

here is a link to jsfiddle I am writing a jquery menu that will meet my needs. I know that there is no need to reinvert the wheel here but it also is a learning experience.

Also, I know the code is ugly but I will restructure and clean it up later once I have it working properly.

My problem is that when i hover over the dropdown links, it disappears. I want it to stay there and the parent menu active. Any help is appreciated.

Here is some code:

<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
</head>
<body>
  <div>
    <ul>
        <li>
            <div class="menu-item"></div>
            <div class="menu-text">text</div>
            <div class="dropdown">
                <div class="dropdown-item"><a href="#">item 1</a></div>
                <div class="dropdown-item"><a href="#">item 2</a></div>
                    <div class="dropdown-item"><a href="#">item 3</a></div>
            </div>
        </li>
        <li><div class="menu-item"></div><div class="menu-text">texttexttext</div></li>
        <li><div class="menu-item"></div><div class="menu-text">tetexttexttexttexttexttexttextxt</div></li>
      </ul>
  </div>
<script>
    $(function() {    
        $("div.menu-text").mouseover(function () {
            $(this).prevAll('div.menu-item').animate({height: '100%'},
                {
                    duration: 700,
                    specialEasing: {height: 'easeOutQuint'}
                }
            );
            if($(this).siblings('div.dropdown').is(':hidden')){
                $(this).siblings('div.dropdown').slideDown();
            }
        });

        $("div.menu-text").mouseout(function () {
             $(this).prevAll('div.menu-item').animate({height: '10px'}, 700);
            if($(this).siblings('div.dropdown').is(':visible')){
                $(this).siblings('div.dropdown').slideUp();     
            }
        });   

        $("div.dropdown-item a").hover(function () {
            $(this).parent().siblings('div.menu-item').css("display","block");
            $(this).parent().css("display","block");
        });    
    }); 
  </script> 
</body>
</html>

Here is the CSS:

li {
    background-color: #ccc;
    position: relative;
    color: #fff;
    z-index: -2;
    float: left;
}

div.menu-item {
    background-color: #000;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 10px;
    z-index: -1;
}

div.menu-text {
    color: #fff;
    margin: 0;
    padding: 20px 10px;    
}

div.dropdown {
    display: none;
    position: absolute;
}

div.dropdown-item a {
    padding: 10px;  
    background-color: #1E4b55;
    display: block;
    white-space: nowrap;
}

There is a lot of room for improvement in your JS, but this will do the trick:

http://jsfiddle.net/bUh4L/16/

$(function() {
    $("li").hover(
        function() {
            $(this).find('div.menu-item').animate({height: '100%'},
                {
                    duration: 700,
                    specialEasing: {
                        height: 'easeOutQuint'
                    }
                });
            if($(this).find('div.dropdown').is(':hidden')) {
                $(this).find('div.dropdown').slideDown();
            }
        },
        function() {
            $(this).find('div.menu-item').animate({height: '10px'}, 700);
            if($(this).find('div.dropdown').is(':visible')){
                $(this).find('div.dropdown').slideUp();     
            }
        }
    );
});

I'm using .hover() which takes two functions, the first for when hover starts, the second for when hover ends. You also need to move this to a parent element (the li ) so the hovering is persisted while over top child elements.

See this: http://jsfiddle.net/Y9knm/

$(function () {
 $("li").hover(
 function () {
    $(this).find('div.menu-item').stop().animate({
        height: '100%'
    }, {
        duration: 700,
        specialEasing: {
            height: 'easeOutQuint'
        }
    });
    $(this).find('div.dropdown').slideDown();
 },

 function () {
    $(this).find('div.menu-item').stop().animate({
        height: '10px'
    }, 700);
    $(this).find('div.dropdown').stop().slideUp();
 });
});

The jsfiddle example is not complete. Requires the jquery easing library. Not included in the jsfiddle example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM