代码之家  ›  专栏  ›  技术社区  ›  vol7ron

jquery:悬停在子元素上时保持其打开状态

  •  4
  • vol7ron  · 技术社区  · 14 年前

    我的问题是相似的,但不同于 jquery-hover-menu-when-hovering-over-child-menu-disappears .

    我最初在 li.item 这有点奇怪,但做了我需要它做的。我将悬停切换到范围,这样事件将在文本块上触发,而不是列表块,它将扩展列表的整个宽度。

    我试图达到的效果是当你徘徊在 ul.sub . 我希望它继续在队列中的动画 span.text 的悬停,显示它,但也保持它的打开状态。

    发生的是老鼠离开了跨度,所以 列项 正在触发触发器的MouseOut部分。


    jsFiddle Page


    HTML


       <ul id="main">
           <li class="head">Title Bar</li>
           <li class="item odd">
               <span class="text">A</span>
               <ul class="sub">
                   <li>1</li>
                   <li>2</li>
               </ul>
           </li>
           <li class="item even">
               <span class="text">B</span>
               <ul class="sub">
                   <li>3</li>
                   <li>4</li>
               </ul>
           </li>
           <li class="item odd">
               <span class="text">C</span>
               <ul class="sub">
                   <li>5</li>
                   <li>6</li>
               </ul>
           </li>
           <li class="item even">
               <span class="text">D</span>
               <ul class="sub">
                   <li>7</li>
                   <li>8</li>
               </ul>
           </li>
       </ul>
    


    CSS


    /* colors used are not for production; they are
       used only to enhance the example's visual distinction */
    
       #main{width:10em;}
       .head,.item{border:1px solid black;padding:.5em;}
       .head{font-weight:bold; background:#336; color:#fff; cursor:pointer;}
       .item{background:#f90;}
       .sub{display:none;}
       .sub li{padding-left:1em;}
       .text,.sub{cursor:pointer;}
    


    JavaScript


       $(document).ready(function(){
          // specific here because of other divs/list items
    
          $('#main li.item span.text').hover(function(){
             $(this).siblings().stop(true,true).toggle('slow');     
          });       
    
          $('li.head').hover(function(){
             $(this).parent().find('ul.sub').stop(true,true).toggle('slow');
          });
       });
    

    编辑:

    我认为沿着这些线的东西是我需要的,但是动画是重新调整时,从潜艇到跨度。

    $(document).ready(function(){
       // specific here because of other divs/list items
    
       $('#main li.item span.text').hover(
           function(){$(this).siblings().stop(false,true).show('slow');}
          ,function(){$(this).siblings().stop(true,true).hide('slow');}     
       );    
    
       $('#main li.item ul.sub').hover(
            function(){$(this).stop(false,true).show();}
           ,function(){$(this).stop(false,true).hide('slow');}
       );    
    
       $('li.head').hover(function(){
          $(this).parent().find('ul.sub').stop(true,true).toggle('slow');
       });
    });
    
    1 回复  |  直到 14 年前
        1
  •  5
  •   Frédéric Hamidi    14 年前

    hover mouseenter mouseleave toggle() show() hide() mouseenter span.text mouseleave li.item

    $(document).ready(function() {
        // specific here because of other divs/list items
        $('#main li.item span.text').mouseenter(function() {
            $(this).siblings().stop(true, true).show('slow');
        });
    
        $('#main li.item').mouseleave(function() {
            $(this).children("ul").stop(true, true).hide('slow');
        });
    
        $('li.head').hover(function() {
            $(this).parent().find('ul.sub').stop(true, true).toggle('slow');
        });
    });