Josh
Josh

Reputation: 11

How do I make this menu collapse on page load?

This is my code. How can I make this menu always be popped out, not just when you click on '#projects'? Change the .click function to what?

$(document).ready(function($) {

$.fn.longDropdown = function(options) { var opts = $.extend({}, $.fn.longDropdown.defaults, options);

// Weird bug to show the correct number of items
opts.visible = (opts.visible + 1);

return this.each(function() {

    // Add down arrow only to menu items with submenus
    // $(".dropdown > li:has('ul')").each(function() {
    //     $(this).find("a:first").append("<img src='images/down-arrow.png' />");
    // });

    $(".dropdown > li").click(function() {

            var $container = $(this),
                $list = $container.find('ul'),
                $anchor = $container.find('a'),

                height = $list.height() * 1.0,

                totalHeight = $container.height() * opts.visible,

                containerOffset = $container.offset().top + $container.height(),
                windowHeight = $(window).height(),

                maxHeight = windowHeight - containerOffset;

            if( totalHeight > maxHeight ) {
                while( $container.height() * opts.visible > maxHeight ) {
                    opts.visible = (opts.visible - 1);
                }
                totalHeight = $container.height() * (opts.visible + 1);
            }

            var multiplier = height / totalHeight;

            // need to save height here so it can revert on mouseout            
            $container.data("origHeight", $container.height());

            // so it can retain it's rollover color all the while the dropdown is open
            $anchor.addClass("hover");

            // make sure dropdown appears directly below parent list item    
            $list
                .show()
                .css({
                    paddingTop: $container.data("origHeight")
                });

            // don't do any animation if list shorter than max
            if (multiplier > 1) {
                $container
                    .css({
                        height: totalHeight,
                        overflow: "hidden"
                    })
                    .mousemove(function(e) {
                        var offset = $container.offset();
                        var relativeY = ((e.pageY - offset.top) * (multiplier)) - ($container.data("origHeight") * multiplier);
                        if (relativeY > $container.data("origHeight")) {
                            $list.css("top", -relativeY + $container.data("origHeight"));
                        };
                    }); 

            }
        }, function() {
            var $el = $(this);

            // put things back to normal
            $el
                .height($(this).data("origHeight"))
                .find("ul")
                .css({ top: 0 })
                .hide()
                .end()
                .find("a")
                .removeClass("hover");

            opts.visible = (opts.visible + 1);
        })
        .filter(':has(ul)')
        .each(function() {
            $(this).find("a:first").append("<img src='" + opts.image + "' />");
        });

});

};

// default options $.fn.longDropdown.defaults = { visible: 4, image: 'images/down-arrow.png' };

})(jQuery);

Thank you.

Upvotes: 1

Views: 760

Answers (2)

prime_number
prime_number

Reputation: 758

This should do the trick: $($('#projects').click());

Upvotes: 0

leepowers
leepowers

Reputation: 38318

Maybe you could take the anonymous function inside $(".dropdown > li").click() and name it, then call it on document load? Something like this:

1) Give the function a name:

 function expandMenu($container) {
  if (typeof $container=='undefined') $container= $(this);
  $list = $container.find('ul'),
  $anchor = $container.find('a'),

   . . . . 
   Rest of the function body
   . . . .
}

2) Bind the function to elements for clicking AND trigger on document load:

$(".dropdown > li").click(expandMenu);

AND

$(document).ready(function() {
 expandMenu($('#id_of_auto_expand_menu'));
});

Something like this should work - that is, refactoring the function.

Upvotes: 1

Related Questions