Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

creating a sliding event on a menu using jquery

l have created a menu that currently has a dropdown down. the dropdown got the code below but what i would want to be able to do is that l want to make the dropdown slide down. so l tried using slideup and down but it doesnt seem to work. the code is below. any ideas how l can make it to perfection? Andrew Chalkley

        $(document).ready(function() {

    $('.myMenu > li').bind('mouseover', openSubMenu);
    $('.myMenu > li').bind('mouseout', closeSubMenu);

    function openSubMenu() {
      $(this).find('ul').css("visibility", "visible").slideDown();

    };

    function closeSubMenu() {
      $(this).find('ul').css("visibility", "hidden").slideUp();

    };

  });

Try inserting the respective functions inside the .bind() method call. Also, the .find() method only locates descendants, so you could just leave it out and use $(this) to apply the .slideDown() and .slideUp() methods directly to the $('.myMenu > li') elements:

$(document).ready(function() {

    $('.myMenu > li').bind('mouseover', function() {
      $(this).css("visibility", "visible").slideDown();
    });

    $('.myMenu > li').bind('mouseout', function() {
      $(this).css("visibility", "hidden").slideUp();
    });

});

I think this will work, but if not, the .bind() method docs are helpful. Let me know how it goes!

that dont seem to work mate

What are your initial visibility settings for .myMenu in your CSS? I would set your .myMenu display setting to css display: none; and remove the javascript .css("visibility", "visible") part of your function.

.myMenu {
  display: none;
}
$(document).ready(function() {

    $('.myMenu > li').bind('mouseover', function() {
      $(this).slideDown();
    });

    $('.myMenu > li').bind('mouseout', function() {
      $(this).slideUp();
    });

});

2 Answers

<ul class="myMenu">
  <li>Menu item
    <ul>
      <li>Sub menu item</li>
      <li>Sub menu item</li>
      <li>Sub menu item</li>
    </ul>
  </li>
  <li>Menu item
      <ul>
      <li>Sub menu item</li>
      <li>Sub menu item</li>
      <li>Sub menu item</li>
    </ul>
  </li>
  <li>Menu item</li>
  <li>Menu item</li>
  <li>Menu item</li>
</ul>
.myMenu li ul{
  display: none;
}

Quick tip to prevent Animation queue Buildup :) Need to use the stop() function.

$(document).ready(function() {

    $('.myMenu li').on('mouseover', function(){

        $(this).find('ul').stop().slideDown();

    }).on('mouseout', function(){

        $(this).find('ul').stop().slideUp();

    });
});

Nice!

Thanks! :) And another quick tip is to use on() method instead of bind(). Here is an article about why: http://www.elijahmanor.com/differences-between-jquery-bind-vs-live-vs-delegate-vs-on/

Try using .mouseover() with a callback $( "#outer" ).mouseover(function() { $( "#log" ).append( "<div>Handler for .mouseover() called.</div>" ); });

In your case you may try:

$('.myMenu > li').mouseover(function(){

alert("Heyo!"); $(this).slideToggle(); });