0

当鼠标悬停在此菜单中的子菜单上时,如何使 :hover 状态保持在顶层?这可能很简单,但我似乎无法让它发挥作用。我试过做 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
}
}
4

1 回答 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>
于 2012-05-24T17:55:49.560 回答