将鼠标悬停在子菜单项上时,保持悬停状态

时间:2012-05-24 17:11:47

标签: javascript jquery menu hover mouseenter

当我在这个菜单中的子菜单上进行鼠标悬停时,我怎样才能将:悬停状态留在顶层?它可能很简单,但我似乎无法使它工作。我尝试过做mouseEnter,但我很可能做得不正确,因为我对jQuery知之甚少。提前谢谢!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Menu</title>


<link rel="stylesheet" type="text/css" href="droplinetabs.css" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script src="droplinemenu.js" type="text/javascript"></script>

<script type="text/javascript">

//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("droplinetabs1")


</script>




</head>

<body>

<div id="droplinetabs1" class="droplinetabs">
<ul id="base">
<li><a href="#">Stuff to Do</a>
  <ul>
  <li class="base"><a href="#">Activities 1.1</a>     
    <ul>
          <li><a href="#">Cricket</a></li>
          <li><a href="#">Rugby</a></li>
          <li><a href="#">Soccer</a></li>
         <li><a href="#"> Football</a></li>
      </ul>
  </li>
  <li><a href="#">Activities 1.2</a>      
        <ul>
          <li><a href="#">Eating</a></li>
          <li><a href="#">Cooking</a></li>
          <li><a href="#">Baking</a></li>
      </ul>
  </li>
  <li><a href="#">Activities 1.3</a>
      <ul>
        <li><a href="#">Music</a></li>
        <li><a href="#">Chess</a></li>
        <li><a href="#">Reading</a></li>
        <li><a href="#">Scribbling</a></li>
      </ul>
    </li>
  </ul>
</li>
<li class="base"><a href="#">More Stuff to Do</a>
  <ul>
  <li><a href="#">Activities 2.1</a>      
    <ul>
        <li><a href="#">Cricket</a></li>
        <li><a href="#">Rugby</a></li>
        <li><a href="#">Soccer</a></li>
        <li><a href="#"> Football</a></li>
      </ul>
  </li>
  <li><a href="#">Activities 2.2</a>      
    <ul>
          <li><a href="#">Eating</a></li>
          <li><a href="#">Cooking</a></li>
          <li><a href="#">Baking</a></li>
   </ul>
  </li>
  <li><a href="#">Activities 2.3</a>
      <ul>
         <li><a href="#">Music</a></li>
         <li><a href="#">Chess</a></li>
         <li><a href="#">Reading</a></li>
         <li><a href="#">Scribbling</a></li>
      </ul>
    </li>
  </ul>
</li>
<li class="base"><a href="#">Even More</a>
  <ul>
  <li><a href="#">Activities 3.1</a>      
    <ul>
      <li><a href="#">Cricket</a></li>
      <li><a href="#">Rugby</a></li>
      <li><a href="#">Soccer</a></li>
      <li><a href="#">Football</a></li>
     </ul>
  </li>
  <li><a href="#">Activities 3.2</a>      
    <ul>
      <li><a href="#">Eating</a></li>
      <li><a href="#">Cooking</a></li>
      <li><a href="#">Baking</a></li>
    </ul>
  </li>
  <li><a href="#">Activities 3.3</a>
    <ul>
      <li><a href="#">Music</a></li>
      <li><a href="#">Chess</a></li>
      <li><a href="#">Reading</a></li>
      <li><a href="#">Scribbling</a></li>
      </ul>
   </li>
</ul>
</li>
</ul>
</div>






</body>
</html>

var droplinemenu={



arrowimage: {classname: 'downarrowclass', src: 'down.gif', leftpadding: 5}, //customize down arrow image
animateduration: {over: 1, out: 1}, //duration of slide in/ out animation, in milliseconds



buildmenu:function(menuid){
    jQuery(document).ready(function($){
        var $mainmenu=$("#"+menuid+">ul")
        var $headers=$mainmenu.find("ul").parent()
        $headers.each(function(i){
            var $curobj=$(this)
            var $subul=$(this).find('ul:eq(0)')
            this._dimensions={h:$curobj.find('a:eq(0)').outerHeight()}
            this.istopheader=$curobj.parents("ul").length==1? true : false
            if (!this.istopheader)
                $subul.css({left:0, top:this._dimensions.h})
            var $innerheader=$curobj.children('a').eq(0)
            $innerheader=($innerheader.children().eq(0).is('span'))? $innerheader.children().eq(0) : $innerheader //if header contains inner SPAN, use that

            $curobj.hover(
                function(e){
                    var $targetul=$(this).children("ul:eq(0)")
                    if ($targetul.queue().length<=1) //if 1 or less queued animations
                        if (this.istopheader)
                            $targetul.css({left: $mainmenu.position().left, top: $mainmenu.position().top+this._dimensions.h})
                        if (document.all && !window.XMLHttpRequest) //detect IE6 or less, fix issue with overflow
                            $mainmenu.find('ul').css({overflow: (this.istopheader)? 'hidden' : 'visible'})
                        $targetul.dequeue().slideDown(droplinemenu.animateduration.over)
                },
                function(e){
                    var $targetul=$(this).children("ul:eq(0)")
                    $targetul.dequeue().slideUp(droplinemenu.animateduration.out)
                }
            ) //end hover
        }) //end $headers.each()
        $mainmenu.find("ul").css({display:'none', visibility:'visible', width:$mainmenu.width()})
    }) //end document.ready
}
}

3 个答案:

答案 0 :(得分:0)

如果你想看到这个Link漂亮的设计能够html / css(不需要javascript)菜单希望你喜欢它。

答案 1 :(得分:0)

$(this).addClass('hover');添加到您的悬停输入功能,将$(this).removeClass('hover');添加到您的悬停功能,并将li.hover{/* css specific to it */}添加到您的CSS。

        $curobj.hover(
            function(e){
                var $targetul=$(this).children("ul:eq(0)")
                                    $(this).addClass('hover');
                if ($targetul.queue().length<=1) //if 1 or less queued animations
                    if (this.istopheader)
                        $targetul.css({left: $mainmenu.position().left, top: $mainmenu.position().top+this._dimensions.h})
                    if (document.all && !window.XMLHttpRequest) //detect IE6 or less, fix issue with overflow
                        $mainmenu.find('ul').css({overflow: (this.istopheader)? 'hidden' : 'visible'})
                    $targetul.dequeue().slideDown(droplinemenu.animateduration.over)
            },
            function(e){
                var $targetul=$(this).children("ul:eq(0)")
                                    $(this).removeClass('hover');
                $targetul.dequeue().slideUp(droplinemenu.animateduration.out)
            }
        ) //end hover

或者,如果你想要一个纯CSS替代品。这是我使用每个网站的那个。

nav ul{
    margin-bottom:10px;
    list-style-image:none
}
nav>ul>li{
    display:inline-block;/* float:left needed for some designs */
    font-size:20px;
    text-align:center;
    position:relative;
    z-index:950
}
nav a{
    text-decoration:none;
    display:block;
    color:#000;
    padding:8px 0
}
nav li ul{
    display:none;
    position:absolute;
    padding:0 8px;
    margin:0
}
nav li:hover ul{display:block}
nav li li{
    font-size:15px;
    float:none;
    width:100%;
    z-index:999
}

请注意,这是一个骨架,并且看起来不漂亮。

HTML也非常简单:

<nav>
    <ul>
        <li><a href="#">top level item</a>
            <ul>
                <li><a href="#">sub-item</a></li>
            </ul>
        </li>
        <li>top level item</li>
    </ul>
</nav>

答案 2 :(得分:0)

这是一个小提琴,我刚刚为另一个问题而建。 http://jsfiddle.net/uA7XD/76/