The accordion don't stop when applying a hover mouse

Viewed 50

I am using the accordion to display the subcategories of each category.

My Problem is that I can't go to the subcategory of a category because it closes after the mouse out.

I want to keep the same architecture of the html:
Currently I have

jQuery(function($) {
  $('ul.r-list h1').hover(function() {
    $(this).next().stop(true, true).slideDown()
  }, function() {
    $(this).next().stop(true, true).slideUp()
  }).find($(this).next()).hide()
});
ul.r-list li ul {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="r-list">
  <li>
    <h1>
      <span class="collapse resp-toggle"><i class="fa fa-caret-right fa-lg"></i></span>
      <a class="category Parent" href="">Parent Category</a>
      <span>(8)</span>
    </h1>
    <ul>
      <li>
        <a class="category sub-category children1" href="">sub Category1</a>
        <span>(5)</span>
      </li>
      <li>
        <a class="category sub-category children2" href="">sub Category2</a>
        <span>(1)</span>
      </li>
      <li>
        <a class="category sub-category children3" href="">sub Category3</a>
        <span>(2)</span>
      </li>
    </ul>
  </li>
</ul>

Expected result:
when I go to child category the animation stop and when I mouse out it automatically closes.

Current result:
when i go to child category the animation didn't stop

Thanks in advance

2 Answers

Listen to the hover event on the whole ul.r-list li so the mouse out event does not fire when you move from h1 to ul. Then in the event handler functions find ul inside current this element and slide it up/down.

jQuery(function ($) {
    $('ul.r-list li').hover(function () {
      $(this).find('ul').stop(true,true).slideDown()
    },function(){
      $(this).find('ul').stop(true,true).slideUp() 
    }).find($(this).next()).hide()
});
ul.r-list li ul {
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="r-list">
 <li>
  <h1>
   <span class="collapse resp-toggle"><i class="fa fa-caret-right fa-lg"></i></span>
   <a class="category Parent" href="">Parent Category</a>
   <span>(8)</span>
  </h1>
  <ul>
   <li>
    <a class="category sub-category children1" href="">sub Category1</a>
    <span>(5)</span>
   </li>
   <li>
    <a class="category sub-category children2" href="">sub Category2</a>
    <span>(1)</span>
   </li>
   <li>
    <a class="category sub-category children3" href="">sub Category3</a>
    <span>(2)</span>
   </li>
  
   
  </ul>
 </li>
</ul>

You were close. Add a class like r-hasSublist to the common LI element (that wraps H1 and the UL sublist). Target that class to keep the hover's mouseenter event active.

Inside the .hover() method you can than use simply $('> ul', this).stop().slideToggle();

jQuery(function($) {

  $('.r-hasSublist').hover(function() {
    $('> ul', this).stop().slideToggle();
  });
  
});
.r-hasSublist ul {
  display: none;
}
<ul class="r-list">
  <li class="r-hasSublist">
    <h1>
      <span class="collapse resp-toggle"><i class="fa fa-caret-right fa-lg"></i></span>
      <a class="category Parent" href="">Parent Category</a>
      <span>(8)</span>
    </h1>
    <ul>
      <li>
        <a class="category sub-category children1" href="">sub Category1</a>
        <span>(5)</span>
      </li>
      <li>
        <a class="category sub-category children2" href="">sub Category2</a>
        <span>(1)</span>
      </li>
      <li>
        <a class="category sub-category children3" href="">sub Category3</a>
        <span>(2)</span>
      </li>
    </ul>
  </li>
</ul>

<script src="//code.jquery.com/jquery-3.3.1.min.js"></script>

Related