0

我有一个带有子菜单的 jquery 菜单。也许太简单了,但我对 jquery 有一点经验

我想在鼠标悬停事件时显示子菜单,并保持它可见,直到选择不同的菜单选项。

我正在使用在http://www.dynamicdrive.com/style/csslibrary/item/jquery_drop_line_tabs/找到的 jquery 菜单

这是代码

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

<script type="text/javascript">

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

</script>
<div id="droplinetabs1" class="droplinetabs">
<ul>
<li><a href="http://www.dynamicdrive.com/"><span>Home</span></a></li>
<li><a href="http://www.dynamicdrive.com/style/"><span>CSS Examples</span></a>
  <ul>
  <li><a href="#">Activities 1</a></li>
  <li><a href="#">Activities 2</a></li>
  <li><a href="#">Activities 3</a>
      <ul>
      <li><a href="#">Water Sports 1</a></li>
      <li><a href="#">Water Sports 1</a></li>
      <li><a href="#">Water Sports 1</a></li>
      <li><a href="#">Water Sports 1</a></li>
      </ul>
    </li>
  <li><a href="#">Activities 4</a></li>
  </ul>
</li>
<li><a href="http://tools.dynamicdrive.com"><span>Tools</span></a></li>
<li><a href="http://www.javascriptkit.com/"><span>JavaScript</span></a>
  <ul>
  <li><a href="#">Traveling 1</a></li>
  <li><a href="#">Traveling 2</a></li>
  <li><a href="#">Traveling 3</a></li>
  <li><a href="#">Traveling 4</a>
      <ul>
      <li><a href="#">Africa 1</a></li>
      <li><a href="#">Africa 2</a></li>
      <li><a href="#">Africa 3</a></li>
      <li><a href="#">Africa 4</a>
          <ul>
          <li><a href="#">Kenya 1</a></li>
          <li><a href="#">Kenya 2</a></li>
          <li><a href="#">Kenya 3</a></li>
          <li><a href="#">Kenya 4</a></li>
          <li><a href="#">Kenya 5</a></li>
          </ul>
        </li>
      </ul>
    </li>
  <li><a href="#">Traveling 5</a></li>
  </ul>
</li>
<li><a href="http://www.cssdrive.com"><span>Gallery</span></a></li>
</ul>
</div>

css文件有这个:

.droplinetabs{
overflow: hidden;
border-bottom: 1px solid gray; /*underline across bottom of main tabs*/
}

.droplinetabs ul{
font: bold 11px Verdana, sans-serif;
margin: 0;
padding: 0;
width: 100%;
list-style: none;
}

.droplinetabs li{
display: inline;
margin: 0 2px 0 0;
padding: 0;
text-transform: uppercase;
}


.droplinetabs a{
float: left;
color: white;
background: #c76023 url(leftedge.gif) no-repeat left top; /*default background color of tabs, left corner image*/
margin: 0 4px 0 0;
padding: 0 0 4px 3px;
text-decoration: none;
letter-spacing: 1px;
}

.droplinetabs a:link, .droplinetabs a::visited, .droplinetabs a:active{
color: white;
}

.droplinetabs a span{
float: left;
display: block;
background: transparent url(rightedge.gif) no-repeat right top; /*right corner image*/
padding: 7px 9px 3px 6px;
cursor: pointer;
}

.droplinetabs a span{
float: none;
}


.droplinetabs a:hover{
background-color: #b05016; /*background color of tabs onMouseover*/
color: white;
}

.droplinetabs a:hover span{
background-color: transparent;
}

/* Sub level menus*/
.droplinetabs ul li ul{
position: absolute;
z-index: 100;
left: 0;
top: 0;
background: #c76023; /*sub menu background color */
visibility: hidden;
}

/* Sub level menu links style */
.droplinetabs ul li ul li a{
font: normal 13px Verdana;
padding: 6px;
padding-right: 8px;
margin: 0;
background: #c76023; /*sub menu background color */
}

.droplinetabs ul li ul li a span{
background: #c76023; /*sub menu background color */
}

.droplinetabs ul li ul li a:hover{ /*sub menu links' background color onMouseover. Add rounded edges in capable browsers */
background: #714421;
-webkit-border-radius: 5px;  
-moz-border-radius: 5px; 
-khtml-border-radius: 5px;  
border-radius: 5px;
}

js文件有这个

/*********************
//* jQuery Drop Line Menu- By Dynamic Drive: http://www.dynamicdrive.com/
//* Last updated: May 9th, 11'
//* Menu avaiable at DD CSS Library: http://www.dynamicdrive.com/style/
*********************/
var droplinemenu={

arrowimage: {classname: 'downarrowclass', src: 'down.gif', leftpadding: 5}, //customize down arrow image
animateduration: {over: 200, out: 100}, //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
            $innerheader.append(
                '<img src="'+ droplinemenu.arrowimage.src
                +'" class="' + droplinemenu.arrowimage.classname
                + '" style="border:0; padding-left: '+droplinemenu.arrowimage.leftpadding+'px" />'
            )
            $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)
                        $targetul.dequeue().show()
                },
                function(e){
                    var $targetul=$(this).children("ul:eq(0)")
                    $targetul.dequeue().hide()
                    //$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
}
}

我认为可能的选择是保留以前的项目名称并将其与新项目名称进行比较

我尝试像下一个一样更改悬停功能,但没有奏效:

$curobj.hover(
                function(e){
                    var $targetul=$(this).children("ul:eq(0)")
                    if($anterior!=$targetul)
                    {
                        $anterior.dequeue().hide()
                    }
                    $anterior=$targetul
                    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)
                        $targetul.dequeue().show()
                },
                function(e){
                    var $targetul=$(this).children("ul:eq(0)")
                    //$targetul.dequeue().hide()
                    //$targetul.dequeue().slideUp(droplinemenu.animateduration.out)
                }
            ) //end hover
4

1 回答 1

2

所以我做了什么:

  1. 在我保持hover当前可见ulvar prevous;

  2. mouseleave保持ul可见,但只要下一个悬停div 没有孩子

Hover

if (previous!= null ) {
    previous.css('display','none');
}

End hover

if (previous!= null ) {
    previous.css('display','block');
}

jsfiddle 上的演示

注意:这个脚本编辑起来相当复杂,所以我建议你找一些更简单的

于 2013-07-04T18:34:09.670 回答