views:

92

answers:

4

I use the following code to slide out a div on a mouse-over, wait a couple of seconds and slide the div back.

$(document).ready(function()
    {$("#divider").mouseover(function () 
     {$("#slider").show("slide", {direction: "left"}, 1000).pause(2000).hide("slide", {direction: "left"}, 1000);}
    );}
);

I'm sure this is simple, but I have limited Javascript/jQuery knowledge. How do I make it so any mouse activity on the trigger is ignored until the animation completes? Right now, if while the div is open you mouse over the trigger area it "remembers" and plays the animation for as many times as you've moved the pointer through the trigger area. Page

+1  A: 

The question has changed... I defer to millsJROSS's answer.

blesh
Sorry, I should have been more clear. I don't want to prevent all actions on the rest of the page, just the trigger area for the sliding div. I don't want to have the animation play multiple times if the user just happen to have moused across the trigger while the div was open or transitioning.
Jason
I discovered that divs with color:transparent wouldn't stop you from clicking through them in IE. so I've adjusted my answer. I've also included code that worked for me. You might not want to do this on document.ready, but the principal will be the same.
blesh
then you'd just change the line for: d=$(document); to d =$(slider)...lol.. it would work, but there's probably a better way... man, I don't even get a +1 for going through the effort of answering a poorly phrased question? haha.
blesh
Sorry. First time here. Not hip to the standard practices ;-) I don't want you to miss out on props for the effort. What do I do?
Jason
lol... don't worry about it, I wiki'ed the answer anyhow (meaning I shouldn't get any credit for it now), since I changed it. ;)
blesh
A: 

Swizzle out the event handler (not sure if this works, but something along these lines):

var omo = function() {
    {$("#divider").mouseover(function () {});}
    slide();
}

var slide = function() {
    {$("#slider").show("slide", {direction: "left"}, 1000)
        .pause(2000)
        .hide("slide", {direction: "left"}, 1000);
    }
    {$("#divider").mouseover(omo);}
}

$(document).ready(function()
    {$("#divider").mouseover(omo);}
);
Dave T.
Same as above. The initial mouse-over doesn't seem to trigger, so it does nothing.
Jason
+3  A: 

I'd suggest removing the event from your divider until the animation is finished, than using the hide callback function to add that event handler back in.

$(document).ready(function() {
    function divider_mouseover() {
        $('#divider').unbind('mouseover');

        $("#slider")
           .show("slide", {direction: "left"}, 1000)
           .pause(2000)
           .hide("slide", {direction: "left"}, 1000, function() {
               $("#divider").mouseover(divider_mouseover);
           });
    };

    $("#divider").mouseover(divider_mouseover);
};
MillsJROSS
+1 This should work.
blesh
I think I understand what you've done, but it's not working because the initial event isn't firing so mousing over doesn't trigger the first slide.
Jason
Nasty don't bind and unbind and rebind. Use a flag. Binding and unbinding adds extra overhead and can slow things down.
PetersenDidIt
+1  A: 

Don't bind and unbind and rebind instead use a flag to decide if you should care about the event.

$(document).ready(function(){
    $("#divider").mouseover(function(){
        var $this = $(this);
        if($this.data('nomouse')) return;
        $this.data('nomouse',true);
        $("#slider")
        .show("slide", {direction: "left"}, 1000)
        .pause(2000)
        .hide("slide", {direction: "left"}, 1000, function() {
            $this.data('nomouse',false);
        });
    });
});

Binding and unbinding was of one Paul Irish's jQuery Anti-Patterns in the yayQuery podcast

PetersenDidIt
Sweet! This works perfect. Thanks very much.
Jason
I agree completely with this answer, +1. My binding/unbinding isn't nearly as snazzy. Thanks for the links!
MillsJROSS