Results 1 to 4 of 4

Thread: [jQuery] Accordion Hover effect bug

  1. #1
    Join Date
    Oct 2008
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default [jQuery] Accordion Hover effect bug

    Heres my full jQuery code for this accordion,

    Code:
      
    $(document).ready(function() {
     	
    	$('div.accordionButton').mouseover(function() {
    		$('div.accordionContent').hide('fast');	
    		$(this).next().slideDown('normal');
    	});
    	
    	
    	$('div.accordionContent').mouseout(function() {
    	        $("div.accordionContent").slideUp('normal');
    	});
    	
    	
    	$("div.accordionContent").hide('fast');
     
    });
    And heres my problem:

    Code:
        
    	$('div.accordionContent').mouseout(function() {
    	        $("div.accordionContent").slideUp('normal');
    	});
    I have it set so that when a user moves the mouse off the div.accordionContent the content slides up.
    In div.accordionContent there are multiple <a> tags listed, and when i hover over one of these <a> tags that are inside the div.accordionContent,
    the content slides up before i get a chance to click on one of the listed <a> links.

    So im assuming this is because moving the mouse onto the <a> tag is technically mousing out of div.accordionContent and therefore sliding the list up.

    My jQuery syntax knowledge is limited, so i will try explain to the best of my ability what i want to do:
    Somehow include the <a> tags in the mouseout function above so that when a user hovers over the <a> tags which are (children of div.accordionContent) the list doesnt slide up,
    because it recognizes that they are part of the div.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Something like:

    Code:
    	$('div.accordionContent').mouseout(function(event) {
    		var r = event.relatedTarget;
    		while(r && r.parentNode){
    			if(this === r){
    				return;
    			}
    			r = r.parentNode;
    		}
    	        $("div.accordionContent").slideUp('normal');
    	});
    Last edited by jscheuer1; 11-15-2009 at 10:58 AM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    RipzCurlz (11-15-2009)

  4. #3
    Join Date
    Oct 2008
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Thumbs up

    wow thats amazing it works!

    i dont understand how but it works!

    Thanks so much jscheuer1 your a Genius!!

    It works perfectly :O it stumped me for a good few hours and I would have never came up with what you came up with haha.

    Cheers!

    ~Ripz

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The relatedTarget property of the jQuery Event Object is a normalization of the various methods used to obtain the element the mouse moves from when a mouseover or to when a mouseout event takes place. In your function this refers to the element itself (the one the event function was assigned to). These are both normal elements (not jQuery $ type arrays). I simply 'walk up the DOM tree' to see if the element is either the relatedTarget (in the case of a bubbling onmouseout event contained within it), or the parent of the relatedTarget (in the case of moving over a contained element triggering the parent's onmouseout event. I'm not sure which or if both were causing the problem, but it doesn't matter - both bases are covered.

    I did a little experimenting, and it appears that both are involved. And I'm not sure if this will make it any clearer, but a jQuery shorthand method of doing the same thing would be:

    Code:
    	$('div.accordionContent').mouseout(function(e) {
    		if($.inArray(e.relatedTarget, $(this).contents().andSelf()) > -1){
    	        	return;
    	        }
    	        $("div.accordionContent").slideUp('normal');
    	});
    Or, even more concise:

    Code:
    	$('div.accordionContent').mouseout(function(e) {
    		if($.inArray(e.relatedTarget, $(this).contents().andSelf()) < 0){
    	        	$("div.accordionContent").slideUp('normal');
    	        }
    	});
    Last edited by jscheuer1; 11-15-2009 at 05:04 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •