Weird behaviour of CSS Dropdown menu

2019-09-11 13:54发布

Please take a look at this link. By hovering over the 'menu', the drop down menu will show, and it pause. Now I try to changing the menu to a TD element and assign an image to this TD element and while hovering over this image, it will change image and will show drop down. Please refer to the code below:

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<style>
li.li_class{
    display: block;
}

ul.dropDown{
    margin:0px;
    padding:0px;
}
#clickable_div {width:166px; background-color:#9c9c9c;display:block;}

#nav_menu{width:166px; height:auto; background-color:#CCC;z-index: 99999; display: none;position: absolute;}

#wrap{ width:166px;}
</style>
</head>

<body>
    <table>
        <tr>
            <td><img src="images/ori_12.png">
            </td>
            <td id='testable'>
                <div id='wrap'>
                    <img src="images/ori_14.png">
                    <div id="nav_menu">
                        <ul class="dropDown">
                            <li class="li_class"><img src="images/ori_12.png"></li>
                            <li class="li_class"><img src="images/ori_14.png"></li>
                            <li class="li_class"><img src="images/ori_15.png"></li>
                            <li class="li_class"><img src="images/ori_16.png"></li>
                        </ul>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                abc
            </td>
            <td>
                def
            </td>
        </tr>
    </table>

</body>
<script>
        $('#testable img')
        .mouseenter(function () {
            this.src = this.src.replace('/ori_', '/hover_');
            $('#nav_menu').stop(true, true).slideDown();
        })
        .mouseleave(function () {
            this.src = this.src.replace('/hover_', '/ori_');
            $('#nav_menu').stop(true, true).slideUp();
        });



        $('#nav_menu li img')
        .mouseover(function () {
            this.src = this.src.replace('/ori_', '/hover_');
        })
        .mouseout(function () {
            this.src = this.src.replace('/hover_', '/ori_');
        });

    </script>
</html> 

When I hover over the image, the image does get replaced and drop down menu does slide down, however, I just couldn't hover over the dropdownlist item as the dropdown menu slide up immediately.

1条回答
叼着烟拽天下
2楼-- · 2019-09-11 14:06

Try

 $('#testable  #wrap').mouseenter(function (e) {
     var anc = $(this).children('img').get(0);
     anc.src = anc.src.replace('/ori_', '/hover_');
     $('#nav_menu').stop(true, true).slideDown();
 }).mouseleave(function (e) {
     var anc = $(this).children('img').get(0);
     anc.src = anc.src.replace('/hover_', '/ori_');
     $('#nav_menu').stop(true, true).slideUp();
 });

Fiddle

mouseout gets triggered when you hover out of the image to hover into the menu. So apply hover events on a wrapper. If this wrapper is not suitable create a wrapper to wrap menu and its image and apply hover effect on it.

查看更多
登录 后发表回答