forked from mehrpadin/Superfish-for-Drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfautomaticwidth.js
73 lines (72 loc) · 2.77 KB
/
sfautomaticwidth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* sf-AutomaticWidth v1.0b - Automatically adjusts the menu width for the jQuery Superfish plugin.
*
* Developer's note:
* Built as a part of the Superfish project for Drupal (http://drupal.org/project/superfish)
* Found any bug? have any cool ideas? contact me right away! http://drupal.org/user/619294/contact
*
* jQuery version: 1.3.x or higher.
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
(function($){
$.fn.sfautomaticwidth = function(){
function automaticWidth(menu){
// This plugin is not useful for vertical menus.
if (menu.hasClass('sf-horizontal') || menu.hasClass('sf-navbar')){
// Adding a helper class.
// Making the main UL as wide as its container.
menu.addClass('sf-automatic-width').css('width','100%');
var
// We need the inner width of the main UL.
menuWidth = menu[0].clientWidth,
// Selecting only the first level parents.
children = menu.children('li'),
// Number of children.
childrenNumber = children.length,
// Calculating the total width of the children based on their outer width.
childrenWidth = 0;
for (var a = 0; a < childrenNumber; a++) {
// Resetting the children width and getting its outer width then.
childrenWidth += children.eq(a).css({width:''}).outerWidth(true);
}
// Is the menu container wider than the menu?
// (It'll be more flexible in later versions.)
if (menuWidth > childrenWidth) {
var
// Calculating how much width should be added to each first level parent.
difference = (((menuWidth - childrenWidth) * 100) / menuWidth) / childrenNumber;
for (var a = 0; a < childrenNumber; a++){
var
child = children.eq(a),
// Calculating and applying the new width to the item.
autoWidth = (((child[0].clientWidth * 100) / menuWidth) + difference) + '%';
child.css({width:autoWidth});
}
}
// Reset the widths if the menu became narrower than its children.
if (menuWidth < childrenWidth) {
for (var a = 0; a < childrenNumber; a++){
children.eq(a).css({width:''});
}
}
}
}
// Return original object to support chaining.
// Although unnecessary because of the way the module uses these plugins.
for (var b = 0; b < this.length; b++) {
var menu = $(this).eq(b);
automaticWidth(menu);
var timer;
$(window).resize(function(){
clearTimeout(timer);
timer = setTimeout(function(){
automaticWidth(menu);
}, 100);
});
}
return this;
};
})(jQuery);