root / htmltest / sites / all / libraries / superfish-version / supersubs.js @ 5fc58a68
1 |
/*
|
---|---|
2 |
* Supersubs v0.2b - jQuery plugin - LAST UPDATE: MARCH 23rd, 2011
|
3 |
* Copyright (c) 2008 Joel Birch
|
4 |
*
|
5 |
* Jan 16th, 2011 - Modified a little in order to work with NavBar menus as well.
|
6 |
*
|
7 |
* Dual licensed under the MIT and GPL licenses:
|
8 |
* http://www.opensource.org/licenses/mit-license.php
|
9 |
* http://www.gnu.org/licenses/gpl.html
|
10 |
*
|
11 |
* This plugin automatically adjusts submenu widths of suckerfish-style menus to that of
|
12 |
* their longest list item children. If you use this, please expect bugs and report them
|
13 |
* to the jQuery Google Group with the word 'Superfish' in the subject line.
|
14 |
*
|
15 |
*/
|
16 |
|
17 |
(function($){ // $ will refer to jQuery within this closure |
18 |
|
19 |
$.fn.supersubs = function(options){ |
20 |
var opts = $.extend({}, $.fn.supersubs.defaults, options); |
21 |
// return original object to support chaining
|
22 |
return this.each(function() { |
23 |
// cache selections
|
24 |
var $$ = $(this); |
25 |
// support metadata
|
26 |
var o = $.meta ? $.extend({}, opts, $$.data()) : opts; |
27 |
// get the font size of menu.
|
28 |
// .css('fontSize') returns various results cross-browser, so measure an em dash instead
|
29 |
var fontsize = $('<li id="menu-fontsize">—</li>').css({ |
30 |
'padding' : 0, |
31 |
'position' : 'absolute', |
32 |
'top' : '-99999em', |
33 |
'width' : 'auto' |
34 |
}).appendTo($$).width(); //clientWidth is faster, but was incorrect here |
35 |
// remove em dash
|
36 |
$('#menu-fontsize').remove(); |
37 |
|
38 |
// Jump on level if it's a "NavBar"
|
39 |
if ($$.hasClass('sf-navbar')) { |
40 |
$$ = $('li > ul', $$); |
41 |
} |
42 |
// cache all ul elements
|
43 |
$ULs = $$.find('ul:not(.sf-megamenu)'); |
44 |
// loop through each ul in menu
|
45 |
$ULs.each(function(i) { |
46 |
// cache this ul
|
47 |
var $ul = $ULs.eq(i); |
48 |
// get all (li) children of this ul
|
49 |
var $LIs = $ul.children(); |
50 |
// get all anchor grand-children
|
51 |
var $As = $LIs.children('a'); |
52 |
// force content to one line and save current float property
|
53 |
var liFloat = $LIs.css('white-space','nowrap').css('float'); |
54 |
// remove width restrictions and floats so elements remain vertically stacked
|
55 |
var emWidth = $ul.add($LIs).add($As).css({ |
56 |
'float' : 'none', |
57 |
'width' : 'auto' |
58 |
}) |
59 |
// this ul will now be shrink-wrapped to longest li due to position:absolute
|
60 |
// so save its width as ems. Clientwidth is 2 times faster than .width() - thanks Dan Switzer
|
61 |
.end().end()[0].clientWidth / fontsize;
|
62 |
// add more width to ensure lines don't turn over at certain sizes in various browsers
|
63 |
emWidth += o.extraWidth; |
64 |
// restrict to at least minWidth and at most maxWidth
|
65 |
if (emWidth > o.maxWidth) { emWidth = o.maxWidth; }
|
66 |
else if (emWidth < o.minWidth) { emWidth = o.minWidth; } |
67 |
emWidth += 'em';
|
68 |
// set ul to width in ems
|
69 |
$ul.css('width',emWidth); |
70 |
// restore li floats to avoid IE bugs
|
71 |
// set li width to full width of this ul
|
72 |
// revert white-space to normal
|
73 |
$LIs.css({
|
74 |
'float' : liFloat,
|
75 |
'width' : '100%', |
76 |
'white-space' : 'normal' |
77 |
}) |
78 |
// update offset position of descendant ul to reflect new width of parent
|
79 |
.each(function(){
|
80 |
var $childUl = $('>ul',this); |
81 |
var offsetDirection = $childUl.css('left')!==undefined ? 'left' : 'right'; |
82 |
$childUl.css(offsetDirection,emWidth);
|
83 |
}); |
84 |
}); |
85 |
|
86 |
}); |
87 |
}; |
88 |
// expose defaults
|
89 |
$.fn.supersubs.defaults = {
|
90 |
minWidth: 9, // requires em unit. |
91 |
maxWidth: 25, // requires em unit. |
92 |
extraWidth: 0 // extra width can ensure lines don't sometimes turn over due to slight browser differences in how they round-off values |
93 |
}; |
94 |
|
95 |
})(jQuery); // plugin code ends
|