/* * dc mega menu - jquery mega menu * copyright (c) 2011 design chemical * * dual licensed under the mit and gpl licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * */ (function($){ //define the defaults for the plugin and how to call it $.fn.dcmegamenu = function(options){ //set default options var defaults = { classparent: 'dc-mega', rowitems: 3, speed: 'fast', effect: 'fade' }; //call in the default otions var options = $.extend(defaults, options); var $dcmegamenuobj = this; //act upon the element that is passed into the design return $dcmegamenuobj.each(function(options){ megasetup(); function megaover(){ var subnav = $('.sub',this); $(this).addclass('mega-hover'); if(defaults.effect == 'fade'){ $(subnav).fadein(defaults.speed); } if(defaults.effect == 'slide'){ $(subnav).slidedown(defaults.speed); } } function megaout(){ var subnav = $('.sub',this); $(this).removeclass('mega-hover'); $(subnav).hide(); } function megasetup(){ $arrow = ''; var classparentli = defaults.classparent+'-li'; var menuwidth = $($dcmegamenuobj).outerwidth(true); $('> li',$dcmegamenuobj).each(function(){ //set width of sub var mainsub = $('> ul',this); var primarylink = $('> a',this); if($(mainsub).length > 0){ $(primarylink).addclass(defaults.classparent).append($arrow); $(mainsub).addclass('sub').wrap('
'); // get position of parent item var position = $(this).position(); parentleft = position.left; if($('ul',mainsub).length > 0){ $(this).addclass(classparentli); $('.sub-container',this).addclass('mega'); $('> li',mainsub).addclass('mega-hdr'); $('.mega-hdr > a').addclass('mega-hdr-a'); // create rows var hdrs = $('.mega-hdr',this); rowsize = parseint(defaults.rowitems); for(var i = 0; i < hdrs.length; i+=rowsize){ hdrs.slice(i, i+rowsize).wrapall(''); } // get sub dimensions & set row height $(mainsub).show(); // calc left position of sub menu // // get width of parent var parentwidth = $(this).width(); // // calc width of sub menu var subwidth = $(mainsub).outerwidth(true); var totalwidth = $(mainsub).parent('.sub-container').outerwidth(true); var containerpad = totalwidth - subwidth; var itemwidth = $('.mega-hdr',mainsub).outerwidth(true); var rowitems = $('.row:eq(0) .mega-hdr',mainsub).length; var inneritemwidth = itemwidth * rowitems; var totalitemwidth = inneritemwidth + containerpad; // set mega header height $('.row',this).each(function(){ $('.mega-hdr:last',this).addclass('last'); var maxvalue = undefined; $('.mega-hdr-a',this).each(function(){ var val = parseint($(this).height()); if (maxvalue === undefined || maxvalue < val){ maxvalue = val; } }); $('.mega-hdr-a',this).css('height',maxvalue+'px'); $(this).css('width',inneritemwidth+'px'); }); // // calc required left margin var marginleft = (totalitemwidth - parentwidth)/2; var subleft = parentleft - marginleft; // if left position is negative set to left margin if(subleft < 0){ $('.sub-container',this).css('left','0'); } else { $('.sub-container',this).css('left',parentleft+'px').css('margin-left',-marginleft+'px'); } // calculate row height $('.row',mainsub).each(function(){ var rowheight = $(this).height(); $('.mega-hdr',this).css('height',rowheight+'px'); $(this).parent('.row').css('height',rowheight+'px'); }); $(mainsub).hide(); } else { $('.sub-container',this).addclass('non-mega').css('left',parentleft+'px'); } } }); // set position of mega dropdown to bottom of main menu var menuheight = $('> li > a',$dcmegamenuobj).outerheight(true); $('.sub-container',$dcmegamenuobj).css({top: menuheight+'px'}).css('z-index','1000'); // hoverintent configuration var config = { sensitivity: 2, // number = sensitivity threshold (must be 1 or higher) interval: 100, // number = milliseconds for onmouseover polling interval over: megaover, // function = onmouseover callback (required) timeout: 400, // number = milliseconds delay before onmouseout out: megaout // function = onmouseout callback (required) }; $('li',$dcmegamenuobj).hoverintent(config); } }); }; })(jquery);