jQuery Help

Discussion in 'HTML & Website Design' started by dieselcatalog, Aug 4, 2010.

  1. #1
    Hello! I installed a jQuery menu on a website that I'm building. Can anyone tell me how to modify this code so that it will close back up when not being hovered on? Currently, once you open it, it does not completely shut up. Here is the website I'm designing: http://corporationmerger.net

    Here is the page I found the script at(I'm using the first Standard Accordian version): http://www.i-marco.nl/weblog/jquery-accordion-3/

    Here is the menu code:

    
    jQuery.fn.initMenu = function() {  
        return this.each(function(){
            var theMenu = $(this).get(0);
            $('.acitem', this).hide();
            $('li.expand > .acitem', this).show();
            $('li.expand > .acitem', this).prev().addClass('active');
            $('li a', this).hover(
                function(e) {
                    e.stopImmediatePropagation();
                    var theElement = $(this).next();
                    var parent = this.parentNode.parentNode;
                    if($(parent).hasClass('noaccordion')) {
                        if(theElement[0] === undefined) {
                            window.location.href = this.href;
                        }
                        $(theElement).slideToggle('normal', function() {
                            if ($(this).is(':visible')) {
                                $(this).prev().addClass('active');
                            }
                            else {
                                $(this).prev().removeClass('active');
                            }    
                        });
                        return false;
                    }
                    else {
                        if(theElement.hasClass('acitem') && theElement.is(':visible')) {
                            if($(parent).hasClass('collapsible')) {
                                $('.acitem:visible', parent).first().slideUp('normal', 
                                function() {
                                    $(this).prev().removeClass('active');
                                }
                            );
                            return false;  
                        }
                        return false;
                    }
                    if(theElement.hasClass('acitem') && !theElement.is(':visible')) {         
                        $('.acitem:visible', parent).first().slideUp('normal', function() {
                            $(this).prev().removeClass('active');
                        });
                        theElement.slideDown('normal', function() {
                            $(this).prev().addClass('active');
                        });
                        return false;
                    }
                }
            }
        );
    });
    };
    
    $(document).ready(function() {$('.menu').initMenu();});
    Code (markup):
     
    dieselcatalog, Aug 4, 2010 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There is also a much simpler version available. Whichever one you choose, add another function inside the hover code for when the mouse leaves:
    
    function initMenu() {
      $('#menu ul').hide();
      $('#menu li a').hover(
        function() {
            $(this).next().slideToggle('normal');	
          },
        function() {
            $(this).next().slideToggle('normal');	
          }
        );
      }
    $(document).ready(function() {initMenu();});
    Code (markup):
     
    Cash Nebula, Aug 4, 2010 IP
  3. myst_dg

    myst_dg Active Member

    Messages:
    224
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Check out the .hover() method of jQuery.
     
    myst_dg, Aug 4, 2010 IP
  4. CSM

    CSM Active Member

    Messages:
    1,047
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    55
    #4
    I can not understand why people are building websites and do not have a clue about what they are doing.

    Ok, I am taking jQuery plugins too. But I know how to deal with them.
     
    CSM, Aug 5, 2010 IP
  5. dieselcatalog

    dieselcatalog Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks guys! Cept CSM, your kinda full of yourself.
     
    dieselcatalog, Aug 5, 2010 IP
  6. CSM

    CSM Active Member

    Messages:
    1,047
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    55
    #6
    Thank you :D
     
    CSM, Aug 5, 2010 IP