views:

44

answers:

3

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.

+1  A: 

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.

pygorex1
Thanks for helping, but I can't get it to work. Here is the page, on the upper right side the scroll menu should be always visible: http://onomadesign.com/wordpress/portfolio/identity-design
Josh
A: 

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

prime_number
where (or instead of what) do i put this? thankyou
Josh
Looking back at it. I mistyped it.$(function() { $('#projects').click(); });place it anywhere in the javascript. the $(function) executes when the document is loaded and ready to be manipulated.
prime_number
it makes sense.. but it still doesn't work, tried it everywhere. could you firebug my page (http://onomadesign.com/wordpress/identity-design) to see if you can get it to work? Much appreciated
Josh
I looked at your page and the div you are interested in is:$('#sub_menu').click()
prime_number
A: 

prime_number, I can't make it happen. I don't know what's wrong.

You are saying I should put $(function() { $('#sub_menu').click(); }); anywhere in the Javascript, right? It's not working.

Josh