I need .slideToggle to work on hover (jQuery)

2019-08-04 21:53发布

问题:

I've got a navigation div that toggles open on click. My client wants the link that opens the div to link to a page, and toggle the div on hover instead (so the link can lead to another page on click).

Here's the code I've got so far:

<script>
$(document).ready(function(){
   $("#drawer").hide();
   $(".toggle-drawer").show();

   $('.toggle-drawer').click(function(){
      $("#drawer").slideToggle();
   });  
});
</script>

Here's the link to the site: http://gearthirty.com (click the "supersite" link to see what I'm talking about).

Thanks in advance!

回答1:

<script>
$(function(){ // DOM ready shorthand

  var $drawer = $("#drawer"); // Cache your elements
  var $togg   = $(".toggle-drawer");

  $drawer.hide();
  $togg.show();

  $togg.hover(function(){
     $drawer.stop().slideToggle();
  }); 

});
</script>