tags:

views:

47

answers:

3

hi StackOverflow! I have a problem with JQuery and the following markup.

                <div id="ParentContainer">
                    <div class="main">
                        <div class="sub1"></div>
                        <div class="sub2">
                            text
                        </div>

                    </div>
                </div>

I need the ParentContainer to fade into view when the cursor hovers over a link (outside of this markup). this works but I need to then use the fadeout method when the cursor moves outside of the ParentContainer div. Is that possible?

Many Thanks,

+2  A: 

Is that possible?

Yes it is!

$('#ParentContainer').bind('mouseleave', function(){
    $(this).fadeOut('fast');
});

Reference: mouseleave

jAndy
http://www.quirksmode.org/dom/events/mouseover.html, Although its better to use mouseleave over mouseout, some major browsers haven't implemented them yet, and i agree that mouseleave is a better way but until all major browsers take it on i would use mouseout.
RobertPitt
@RobertPitt: I'm not 100% sure, but I think jQuery does the cross-browser work for you in that case.
jAndy
@jAndy, Possibly but as binding events is the part of the JavaScript engine I would of thought that it would attach the event regardless of the browser type... Not 100% sure myself.
RobertPitt
@RobertPitt - jquery does indeed normalize this, it's an IE behavior that was desirable over `mouseout`, jQuery makes it available on all browsers, `.mouseleave()` and `.hover()` also bind to this event.
Nick Craver
ok then, thanks for the info i know as well know! :)
RobertPitt
A: 

Try:

$('#ParentContainer').mouseleave(function(){
    $(this).fadeOut('slow');
});
Sarfraz
A: 

Another variant:

$("#ParentContainer").mouseout 
(
    function () {$(this).fadeOut ("fast"); }
);

jQuery makes this work with all supported browsers.

Note from the docs:

The **mouseleave event differs from mouseout** in the way it handles
event bubbling. If mouseout were used in this example, then when the
mouse pointer moved out of the Inner element, the handler would be
triggered. This is *usually* undesirable behavior. The mouseleave event,
on the other hand, only triggers its handler when the mouse leaves the
element it is bound to, not a descendant. So in this example, the
handler is triggered when the mouse leaves the Outer  element, but not
the Inner element.
Brock Adams
`mouseout` as opposed to `mouseleave` will fire *when entering a child* as well...which you wouldn't want here. If it helps, `.hover()` maps to `mouseenter` and `mouseleave`, rather than `mouseover` and `mouseout` :)
Nick Craver
@Nick Craver: Interesting. Neither my tests not the jQuery documentation (http://www.jqapi.com/#p=mouseout), seem to support that claim. Do you have a reference?
Brock Adams
@Brock - The [`mouseleave` documentation](http://api.jquery.com/mouseleave/) ...or test it yourself, hover between child and parent to see it fire while still in the parent: http://jsfiddle.net/mTa38/
Nick Craver
@Nick I misread your first comment, as it says exactly what the docs and my amended answer say. Sorry. I got interrupted and thought it said something else. Anywho, the `mouseout` behavior may be desired if the child node is a clickable element. The `mouseleave` answer was already posted. That's why I started this answer with **"Another variant"**.
Brock Adams