diff --git a/index.html b/index.html index 3dcfff3..2838692 100644 --- a/index.html +++ b/index.html @@ -5,256 +5,256 @@ - BaseDemo - HorizontalNav - - - - - - - - - - - - - - - - + BaseDemo - HorizontalNav + + + + + + + + + + + + + + + + -
-
- - - -
-

HorizontalNav

-

HorizontalNav is a jQuery plugin that spans a horizontal navigation to fit the full width of it's container. If you've ever had to create this effect on a project, you'll know it's pretty annoying to do. But this plugin makes it easy and adds support for IE7.

-

-
- -
- -
-
-

Example One

-

This is an example so you can see the difference between a horizontal navigation with no fixed widths and then with a navigation that has the plugin applied to it. There are no requirements to how you style the navigation and adding padding, margins or borders to any of the elements won’t break the effect.

-
+
+
+ + + +
+

HorizontalNav

+

HorizontalNav is a jQuery plugin that spans a horizontal navigation to fit the full width of it's container. If you've ever had to create this effect on a project, you'll know it's pretty annoying to do. But this plugin makes it easy and adds support for IE7.

+

+
+ +
+ +
+
+

Example One

+

This is an example so you can see the difference between a horizontal navigation with no fixed widths and then with a navigation that has the plugin applied to it. There are no requirements to how you style the navigation and adding padding, margins or borders to any of the elements won’t break the effect.

+
- -
-
- -

Navigation with horizontalNav disabled

- -
-

Navigation with horizontalNav enabled

- - -
+ +
+
+ +

Navigation with horizontalNav disabled

+ +
+

Navigation with horizontalNav enabled

+ + +
-
+
// When document is ready...
 $(document).ready(function() {
-	// Call horizontalNav on the navigations wrapping element
-	$('.full-width').horizontalNav({});
+    // Call horizontalNav on the navigations wrapping element
+    $('.full-width').horizontalNav({});
 });
-
+
-
+
<nav class="horizontal-nav full-width">
-	<ul>
-		<li><a href="#">Navigation Item</a></li>
-		<li><a href="#">Work</a></li>
-		<li><a href="#">Blog</a></li>
-		<li><a href="#">About</a></li>
-		<li><a href="#">Contact</a></li>
-	</ul>
+    <ul>
+        <li><a href="#">Navigation Item</a></li>
+        <li><a href="#">Work</a></li>
+        <li><a href="#">Blog</a></li>
+        <li><a href="#">About</a></li>
+        <li><a href="#">Contact</a></li>
+    </ul>
 </nav>
-
+
-
+
// There is no required CSS for this plugin to work properly
 // but here is what is being used to style this demo
 .horizontal-nav {
-	background: #efefef;
-	border-radius: 6px;
+    background: #efefef;
+    border-radius: 6px;
 }
 .horizontal-nav ul {
-	background: #128F9A;
-	float: left;
-	text-align: center;
-	border-radius: 6px;
-	border: 1px solid #0e7079;
+    background: #128F9A;
+    float: left;
+    text-align: center;
+    border-radius: 6px;
+    border: 1px solid #0e7079;
 }
 .horizontal-nav ul li {
-	float: left;
-	border-left: 1px solid #0e7079;
+    float: left;
+    border-left: 1px solid #0e7079;
 }
 .horizontal-nav ul li:first-child {
-	border-left: 0 none;
+    border-left: 0 none;
 }
 .horizontal-nav ul li a {
-	display: block;
-	padding: 10px 20px;
-	color: #fff;
-	border-top: 1px solid rgba(255,255,255, 0.25);
-	border-left: 1px solid rgba(255,255,255, 0.25);
+    display: block;
+    padding: 10px 20px;
+    color: #fff;
+    border-top: 1px solid rgba(255,255,255, 0.25);
+    border-left: 1px solid rgba(255,255,255, 0.25);
 }
 .horizontal-nav ul li:first-child a {
-	border-left: 0 none;
+    border-left: 0 none;
 }
 .horizontal-nav ul li a:hover {
-	background: #12808a;
+    background: #12808a;
 }
 .horizontal-nav ul li:first-child a {
-	border-top-left-radius: 6px;
-	border-bottom-left-radius: 6px;
+    border-top-left-radius: 6px;
+    border-bottom-left-radius: 6px;
 }
 .horizontal-nav ul li:last-child a {
-	border-top-right-radius: 6px;
-	border-bottom-right-radius: 6px;
+    border-top-right-radius: 6px;
+    border-bottom-right-radius: 6px;
 }
-
+
-
+
-
- -
- -
-
-

Available Options

-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
KeyDefault valueDescription
responsivetrueThis option will allow the navigation to auto adjust when the window is resized.
responsiveDelay100The amount of time to wait before re-adjusting the navigation on window resize. Value is set in milliseconds.
tableDisplaytrueEnables modern browsers to use display: table or set to false to manually calculate the widths of list items.
-
-
- -
- - - -
+
+ +
+ +
+
+

Available Options

+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
KeyDefault valueDescription
responsivetrueThis option will allow the navigation to auto adjust when the window is resized.
responsiveDelay100The amount of time to wait before re-adjusting the navigation on window resize. Value is set in milliseconds.
tableDisplaytrueEnables modern browsers to use display: table or set to false to manually calculate the widths of list items.
+
+
+ +
+ + + +
\ No newline at end of file diff --git a/jquery.horizontalNav.js b/jquery.horizontalNav.js index b85dbed..a008355 100644 --- a/jquery.horizontalNav.js +++ b/jquery.horizontalNav.js @@ -6,136 +6,136 @@ * Author URL: http://sebnitu.com */ (function($) { - - $.fn.horizontalNav = function(options) { - - // Extend our default options with those provided. - var opts = $.extend({}, $.fn.horizontalNav.defaults, options); - - return this.each(function () { - - // Save our object - var $this = $(this); - - // Build element specific options - // This lets me access options with this syntax: o.optionName - var o = $.meta ? $.extend({}, opts, $this.data()) : opts; - - // Save the wrapper. The wrapper is the element that - // we figure out what the full width should be - if ($this.is('ul')) { - var ul_wrap = $this.parent(); - } else { - var ul_wrap = $this; - } - - // let's append a clearfixing element to the ul wrapper - ul_wrap.css({ 'zoom' : '1' }).append('
'); - $('.clearHorizontalNav').css({ - 'display' : 'block', - 'overflow' : 'hidden', - 'visibility' : 'hidden', - 'width' : 0, - 'height' : 0, - 'clear' : 'both' - }); - - // Grab elements we'll need and add some default styles - var ul = $this.is('ul') ? $this : ul_wrap.find('> ul'), // The unordered list element - li = ul.find('> li'), // All list items - li_last = li.last(), // Last list item - li_count = li.size(), // The number of navigation elements - li_a = li.find('> a'); // Remove padding from the links - - // If set to responsive, re-construct after every browser resize - if ( o.responsive === true ) { - // Only need to do this for IE7 and below - // or if we set tableDisplay to false - if ( (o.tableDisplay != true) || ($.browser.msie && parseInt($.browser.version, 10) <= 7) ) { - resizeTrigger( _construct, o.responsiveDelay ); - } - } - - // Initiate the plugin - _construct(); - - // Returns the true inner width of an element - // Essentially it's the inner width without padding. - function trueInnerWidth(element) { - return element.innerWidth() - ( - parseInt(element.css('padding-left')) + parseInt(element.css('padding-right')) - ); - } - - // Call funcion on browser resize - function resizeTrigger(callback, delay) { - // Delay before function is called - delay = delay || 100; - // Call function on resize - var resizeTimer; - $(window).resize(function() { - clearTimeout(resizeTimer); - resizeTimer = setTimeout(function() { - callback(); - }, delay); - }); - } - - // The heavy lifting of this plugin. This is where we - // find and set the appropriate widths for list items - function _construct() { - - if ( (o.tableDisplay != true) || ($.browser.msie && parseInt($.browser.version, 10) <= 7) ) { - - // IE7 doesn't support the "display: table" method - // so we need to do it the hard way. - - // Add some styles - ul.css({ 'float' : 'left' }); - li.css({ 'float' : 'left', 'width' : 'auto' }); - li_a.css({ 'padding-left' : 0, 'padding-right' : 0 }); - - // Grabbing widths and doing some math - var ul_width = trueInnerWidth(ul), - ul_width_outer = ul.outerWidth(true), - ul_width_extra = ul_width_outer - ul_width, - - full_width = trueInnerWidth(ul_wrap), - extra_width = (full_width - ul_width_extra) - ul_width, - li_padding = Math.floor( extra_width / li_count ); - - // Cycle through the list items and give them widths - li.each(function(index) { - var li_width = trueInnerWidth( $(this) ); - $(this).css({ 'width' : (li_width + li_padding) + 'px' }); - }); - - // Get the leftover pixels after we set every itms width - var li_last_width = trueInnerWidth(li_last) + ( (full_width - ul_width_extra) - trueInnerWidth(ul) ); - // I hate to do this but for some reason Firefox (v13.0) and IE are always - // one pixel off when rendering. So this is a quick fix for that. - if ($.browser.mozilla || $.browser.msie) { - li_last_width = li_last_width - 1; - } - // Add the leftovers to the last navigation item - li_last.css({ 'width' : li_last_width + 'px' }); - - } else { - // Every modern browser supports the "display: table" method - // so this is the best way to do it for them. - ul.css({ 'display' : 'table', 'float' : 'none', 'width' : '100%' }); - li.css({ 'display' : 'table-cell', 'float' : 'none' }); - } - } - - }); // @end of return this.each() + + $.fn.horizontalNav = function(options) { + + // Extend our default options with those provided. + var opts = $.extend({}, $.fn.horizontalNav.defaults, options); + + return this.each(function () { + + // Save our object + var $this = $(this); + + // Build element specific options + // This lets me access options with this syntax: o.optionName + var o = $.meta ? $.extend({}, opts, $this.data()) : opts; + + // Save the wrapper. The wrapper is the element that + // we figure out what the full width should be + if ($this.is('ul')) { + var ul_wrap = $this.parent(); + } else { + var ul_wrap = $this; + } + + // let's append a clearfixing element to the ul wrapper + ul_wrap.css({ 'zoom' : '1' }).append('
'); + $('.clearHorizontalNav').css({ + 'display' : 'block', + 'overflow' : 'hidden', + 'visibility' : 'hidden', + 'width' : 0, + 'height' : 0, + 'clear' : 'both' + }); + + // Grab elements we'll need and add some default styles + var ul = $this.is('ul') ? $this : ul_wrap.find('> ul'), // The unordered list element + li = ul.find('> li'), // All list items + li_last = li.last(), // Last list item + li_count = li.size(), // The number of navigation elements + li_a = li.find('> a'); // Remove padding from the links + + // If set to responsive, re-construct after every browser resize + if ( o.responsive === true ) { + // Only need to do this for IE7 and below + // or if we set tableDisplay to false + if ( (o.tableDisplay != true) || ($.browser.msie && parseInt($.browser.version, 10) <= 7) ) { + resizeTrigger( _construct, o.responsiveDelay ); + } + } + + // Initiate the plugin + _construct(); + + // Returns the true inner width of an element + // Essentially it's the inner width without padding. + function trueInnerWidth(element) { + return element.innerWidth() - ( + parseInt(element.css('padding-left')) + parseInt(element.css('padding-right')) + ); + } + + // Call funcion on browser resize + function resizeTrigger(callback, delay) { + // Delay before function is called + delay = delay || 100; + // Call function on resize + var resizeTimer; + $(window).resize(function() { + clearTimeout(resizeTimer); + resizeTimer = setTimeout(function() { + callback(); + }, delay); + }); + } + + // The heavy lifting of this plugin. This is where we + // find and set the appropriate widths for list items + function _construct() { + + if ( (o.tableDisplay != true) || ($.browser.msie && parseInt($.browser.version, 10) <= 7) ) { + + // IE7 doesn't support the "display: table" method + // so we need to do it the hard way. + + // Add some styles + ul.css({ 'float' : 'left' }); + li.css({ 'float' : 'left', 'width' : 'auto' }); + li_a.css({ 'padding-left' : 0, 'padding-right' : 0 }); + + // Grabbing widths and doing some math + var ul_width = trueInnerWidth(ul), + ul_width_outer = ul.outerWidth(true), + ul_width_extra = ul_width_outer - ul_width, + + full_width = trueInnerWidth(ul_wrap), + extra_width = (full_width - ul_width_extra) - ul_width, + li_padding = Math.floor( extra_width / li_count ); + + // Cycle through the list items and give them widths + li.each(function(index) { + var li_width = trueInnerWidth( $(this) ); + $(this).css({ 'width' : (li_width + li_padding) + 'px' }); + }); + + // Get the leftover pixels after we set every itms width + var li_last_width = trueInnerWidth(li_last) + ( (full_width - ul_width_extra) - trueInnerWidth(ul) ); + // I hate to do this but for some reason Firefox (v13.0) and IE are always + // one pixel off when rendering. So this is a quick fix for that. + if ($.browser.mozilla || $.browser.msie) { + li_last_width = li_last_width - 1; + } + // Add the leftovers to the last navigation item + li_last.css({ 'width' : li_last_width + 'px' }); + + } else { + // Every modern browser supports the "display: table" method + // so this is the best way to do it for them. + ul.css({ 'display' : 'table', 'float' : 'none', 'width' : '100%' }); + li.css({ 'display' : 'table-cell', 'float' : 'none' }); + } + } + + }); // @end of return this.each() - }; - - $.fn.horizontalNav.defaults = { - responsive : true, - responsiveDelay : 100, - tableDisplay : true - }; - + }; + + $.fn.horizontalNav.defaults = { + responsive : true, + responsiveDelay : 100, + tableDisplay : true + }; + })(jQuery); \ No newline at end of file