jQuery:当鼠标悬停在它上面时,保持切换的子元素

vol*_*ron 4 jquery jquery-ui jquery-animate

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

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

我想要实现的效果是悬停时ul.sub.我希望它继续从span.text悬停的队列中动画,它正在显示它,但也保持打开状态.

发生的事情是鼠标离开了跨度,所以li.item它触发了触发器的鼠标输出部分.


jsFiddle


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>
Run Code Online (Sandbox Code Playgroud)


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;}
Run Code Online (Sandbox Code Playgroud)


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');
      });
   });
Run Code Online (Sandbox Code Playgroud)

编辑:

我认为沿着这些方向的东西是我需要的东西,但是当从sub到span时动画被重新激活.

$(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');
   });
});
Run Code Online (Sandbox Code Playgroud)

Fré*_*idi 5

悬停行为分为两个组成部分:mouseentermouseleave.还将toggle()拆分为show()hide().绑定mouseenterspan.textmouseleaveli.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');
    });
});
Run Code Online (Sandbox Code Playgroud)

这样,悬停不是由空格触发的,这就是你想要的.