Home Ask Login Register

Developers Planet

Your answer is one click away!

Luke February 2016

Hover action on Acordeon menu

I'm searching for a solution how to open and close the accordeon by hoovering over the parent links? I know i could add an hover-method but it always closes the accordeon when I remove the cursor. I should only close a menu when I'm hovering an a other item.

Would be great if someone could give me a hint!

$(document).ready(function () {
  $('#nav > li > a').click(function(){
    if ($(this).attr('class') != 'active'){
      $('#nav li ul').slideUp();
      $(this).next().slideToggle();
      $('#nav li a').removeClass('active');
      $(this).addClass('active');
    }
  });
  if($(".active").parent().parent().is('#nav')){
  		$(".active").next().slideToggle();
  }else{
  	$(".active").parents('ul').siblings('a').click();
  }
 
});
#nav {
    float: left;
    width: 280px;
}
#nav li a {
    display: inline-block;
    padding: 10px 15px;
    text-decoration: none;
    color: #000;
    list-style:none;
}
#nav li a:hover, #nav li a.active {
}
#nav li ul {
    display: none; // used to hide sub-menus
     list-style:none;
}
#nav li ul li a {
    padding: 10px 25px;
}
ul{
  list-style:none;
  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id="nav">
  <li><a href="#" >Item 1</a>
    <ul>
      <li><a href="#" class="active">Sub-Item 1 a</a></li>
      <li><a href="#">Sub-Item 1 b</a></li>
      <li><a href="#">Sub-Item 1 c</a></li>
    </ul>
  </li>
  <li><a href="#">Item 2</a>
    <ul>
      <li        

Answers


dev35000 February 2016

$(document).ready(function () {
  $('#nav > li > a').click(function(){
    if ($(this).attr('class') != 'active'){
      $('#nav li ul').slideUp();
      $(this).next().slideToggle();
      $('#nav li a').removeClass('active');
      $(this).addClass('active');
    }else{
      $('#nav li a').removeClass('active');
      $(this).addClass('active');
       $(this).next().slideToggle();
    }
  });

    $('#nav > li > a').hover(function(){
    if ($(this).attr('class') != 'active'){
      $('#nav li ul').slideUp();
      $(this).next().slideToggle();
      $('#nav li a').removeClass('active');
      $(this).addClass('active');
    }
  });



  if($(".active").parent().parent().is('#nav')){
        $(".active").next().slideToggle();
  }else{
    $(".active").parents('ul').siblings('a').click();
  }

});

Post Status

Asked in February 2016
Viewed 2,490 times
Voted 13
Answered 1 times

Search




Leave an answer


Quote of the day: live life

Devs Planet ®

2014-2016 www.devsplanet.com

Devs Planet © all rights reserved

Search