views:

89

answers:

3

I setup a jquery dropdown menu that works perfect. It drops down when a user rolls over a link. The problem is that when the user rolls over the content area of the drop down menu, it slides back up. I need to set up the code so that the slidedown box remains in the down position while the user's cursor is still over it.

Here is my HTML:

<ul id="tabnav"> 
<li class="tab2"><a href="index2.html" class="btn-slide">My Leases</a></li> 
</ul>   

<div id="leases"> 
<!-- slide down content here --> 
</div>

JS Trigger:

<script type="text/javascript"> 
$(document).ready(function(){
  $(".btn-slide").hover(function(){
    $("#leases").slideToggle("medium");
    $(this).toggleClass("active"); 
    return false;
  });
});
</script>

Any ideas?

EDIT: Here is a link to page in question: http://designvillain.com/testbed/600/601.html

+2  A: 

The .hover() binds two events, mouseenter and mouseleave.

I would instead go granular and use the mouseenter() on the .btn-slide and the mouseleave() on the .leases

$(function()
{    
    $(".btn-slide").mouseenter(function(){ 
        $("#leases").slideToggle("medium"); 
        $(this).toggleClass("active");
    });
    $("#leases").mouseleave(function(){
      $(".btn-slide").toggleClass("active");
      $(this).slideToggle("medium"); 
    });
});

EDIT: Note, if the mouse never enters the #leases div, it will not get the mouseleave, and you may need to consider that.

EDIT2: fix my bad finger typing of funciton to function

Mark Schultheiss
I want to initially trigger the drop-down with a link rollover: `<a href="index2.html" class="btn-slide">My Leases</a>` With this solution, I am having trouble targeting the link rollover.
Thomas
you could move the event to the li instead of $(".btn-slide") use $(".tab2").
Mark Schultheiss
I tried that as well and the dropdown menu still doesn't appear. Should I set it to display: block or something?
Thomas
probably css of display:block; Does it have CSS height? that would be needed...
Mark Schultheiss
I did a little looking around and display:none; but WITH height:what you want is needed like css height:200px;
Mark Schultheiss
I can't set the dimensions because I need the hit area to change dynamically with the width/height of the link text. I uploaded the file I am working on here:http://designvillain.com/testbed/600/601.htmlI have already implemented your code changes, but to no result.
Thomas
THIS IS MY FAULT: funciton is misspelled. Editing... You should also enclose it in the jQuery wrapper.
Mark Schultheiss
Ok, I just implemented the update code at the link above, but it is still not firing.
Thomas
What is the CSS #leases { height: 100%}?
Mark Schultheiss
A: 

I assume the div is hidden on page load and you want it to show when you hover over the link? Change toggle to down...

$(document).ready(function(){
$("#leases").hide();
$(".btn-slide").hover(function(){
    $("#leases").slideDown("medium");
    $(this).toggleClass("active"); 
return false;
});
});

Does it need to slide back up sometime?

Capt Otis
Yeah, it needs to slide back up when the user rolls off #leases
Thomas
Then I would have to say go with Mark Schultheiss' method.
Capt Otis
+1  A: 

I'm not sure if this is what you want, but I'll just drop it here. It waits 500ms before sliding up #leases, and only when appropriate

var isMousedOver;

var hideDropdown = function(a) {
        setTimeout( function() {
            if (isMousedOver) return;
            $("#leases").slideUp("medium");
            $(a).removeClass("active");
        }, 500);
}

$(".btn-slide").hover(
    function(){
        $("#leases").stop(true,true).slideDown("medium");
        isMousedOver = true;
        $(".btn-slide").removeClass("active");
        $(this).addClass("active");
        var that = this;
        $("#leases").data("mouseoutfn", function() { hideDropdown(that) });
    },
    function(){
        isMousedOver = false;
        hideDropdown(this);
    }
);

$("#leases").hover(
    function() {
        isMousedOver = true;
    },
    function() {
        isMousedOver = false;
        $(this).data("mouseoutfn")();
    }
);

Here's a demo: http://jsfiddle.net/mMRZc/

Simen Echholt
Not my exact choice for implementation, but the general idea is there.
Ryan Kinal
I like this solution, but I just copy and pasted it and it doesn't seem to fire on rollover. I can't see any obvious problems with your code...Firebug isn't returning any errors, either.
Thomas
Added a demo to show how it could work :)
Simen Echholt
That did it - thanks!
Thomas
This is a decent answer!
Mark Schultheiss
NOTE: I tested my solution using the same test page (just replaced the code part), seems to work also. I always forget the jsfiddle thing, good idea :)
Mark Schultheiss