0

我知道这个问题已经被问过很多次了,但我不能让我的下拉菜单与我目前所拥有的一起工作。如果我设置菜单显示的延迟,就会出现问题,而没有 setTimeout,菜单就可以正常工作。有人可以看看并提出解决方案吗?我也在尝试它在 IE6 中工作。

这是一个代码副本:

<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8">

<style>
    body {margin:60px; font-family: sans-serif; font-size:12px;}
    ul,ul ul {margin:0;padding:0;} 
    ul li {
        position:relative;
        list-style-type: none;
        padding:5px 0;
        margin:0 10px;
        cursor:pointer;
        border-bottom:1px solid #ddd;
        display:inline-block;
        float:left;     
    }
    .submenu {
        background:#eee;
        display:none;
        position:absolute;
        width:200px;        
        top:25px;
        left:0;
        margin-left:-5px;
    }
    .hover {
        display:block !important;
    }
</style>
</head>
<body>

<nav id="navigation">
<ul>
    <li class="drop">Option 1
        <div class="submenu">
            <ul>
                <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
                <li>Aliquam tincidunt mauris eu risus.</li>
                <li>Vestibulum auctor dapibus neque.</li>
                <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
            </ul>          
        </div>
    </li>
    <li class="drop">Option 2
    <div class="submenu">
            <ul>
                <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
                <li>Aliquam tincidunt mauris eu risus.</li>
                <li>Vestibulum auctor dapibus neque.</li>
                <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
            </ul>          
        </div>
    </li>
</ul>
</nav>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script>

$(document).ready(function(){

    (function(){

        var topNav = $('#navigation'),
            drop = topNav.find('.drop'),
            myTimer;                

        drop.hover(function(){                  
            obj = $(this);                      
            subMenu = obj.find('.submenu');         
            subMenu.addClass('hover');  
            clearTimeout(myTimer);              
        },function(){
            myTimer = setTimeout(function(){                
                subMenu.removeClass('hover');   
            },300);                                             
        });     

    })();   

});



</script>

</body>
</html>
4

2 回答 2

1

我能够解决它。演示在这里http://jsbin.com/owoyon/3/edit

var topNav = $('#navigation'),
    drop = topNav.find('.drop'),
    myTimer;

drop.hover(function () {
    var obj = $(this);
    var subMenu = obj.find('.submenu');
    clearTimeout(myTimer);
    if (subMenu.hasClass('hover')) {
        //do nothing
    } else {
        drop.find('.submenu').removeClass('hover');
        subMenu.addClass('hover');
    }
}, function () {
    myTimer = setTimeout(function () {
        drop.find('.submenu').removeClass('hover');
    }, 300);
});
于 2012-10-30T12:57:34.937 回答
0

我修改了你的 JS 代码并试试这个

var topNav = $('#navigation'),
            drop = topNav.find('.drop'),
            myTimer, subMenu;                

$('#navigation .drop').each(function(){
    var obj = $(this);
    obj.hover(function(){

        subMenu = obj.find('.submenu');
        subMenu.addClass('hover');
    },function(){
            myTimer = setTimeout(function(){                
                obj.find('.submenu').removeClass('hover');    
            },300);                                                   
     });
})

演示:http: //jsfiddle.net/bvhk2/1/

于 2012-10-30T12:51:53.027 回答