1;(function($, window, document, undefined) { 2 var pluginName = 'autoHidingNavbar', 3 $window = $(window), 4 $document = $(document), 5 _scrollThrottleTimer = null, 6 _resizeThrottleTimer = null, 7 _throttleDelay = 70, 8 _lastScrollHandlerRun = 0, 9 _previousScrollTop = null, 10 _windowHeight = $window.height(), 11 _visible = true, 12 _hideOffset, 13 defaults = { 14 disableAutohide: false, 15 showOnUpscroll: true, 16 showOnBottom: true, 17 hideOffset: 'auto', // "auto" means the navbar height 18 animationDuration: 200, 19 navbarOffset: 0 20 }; 21 22 function AutoHidingNavbar(element, options) { 23 this.element = $(element); 24 this.settings = $.extend({}, defaults, options); 25 this._defaults = defaults; 26 this._name = pluginName; 27 this.init(); 28 } 29 30 function hide(autoHidingNavbar) { 31 if (!_visible) { 32 return; 33 } 34 35 autoHidingNavbar.element.addClass('navbar-hidden').animate({ 36 top: -1 * parseInt(autoHidingNavbar.element.css('height'), 10) + autoHidingNavbar.settings.navbarOffset 37 }, { 38 queue: false, 39 duration: autoHidingNavbar.settings.animationDuration 40 }); 41 42 $('.dropdown.open .dropdown-toggle', autoHidingNavbar.element).dropdown('toggle'); 43 44 _visible = false; 45 46 autoHidingNavbar.element.trigger('hide.autoHidingNavbar'); 47 } 48 49 function show(autoHidingNavbar) { 50 if (_visible) { 51 return; 52 } 53 54 autoHidingNavbar.element.removeClass('navbar-hidden').animate({ 55 top: 0 56 }, { 57 queue: false, 58 duration: autoHidingNavbar.settings.animationDuration 59 }); 60 _visible = true; 61 62 autoHidingNavbar.element.trigger('show.autoHidingNavbar'); 63 } 64 65 function detectState(autoHidingNavbar) { 66 var scrollTop = $window.scrollTop(), 67 scrollDelta = scrollTop - _previousScrollTop; 68 69 _previousScrollTop = scrollTop; 70 71 if (scrollDelta < 0) { 72 if (_visible) { 73 return; 74 } 75 76 if (autoHidingNavbar.settings.showOnUpscroll || scrollTop <= _hideOffset) { 77 show(autoHidingNavbar); 78 } 79 } 80 else if (scrollDelta > 0) { 81 if (!_visible) { 82 if (autoHidingNavbar.settings.showOnBottom && scrollTop + _windowHeight === $document.height()) { 83 show(autoHidingNavbar); 84 } 85 return; 86 } 87 88 if (scrollTop >= _hideOffset) { 89 hide(autoHidingNavbar); 90 } 91 } 92 93 } 94 95 function scrollHandler(autoHidingNavbar) { 96 if (autoHidingNavbar.settings.disableAutohide) { 97 return; 98 } 99 100 _lastScrollHandlerRun = new Date().getTime(); 101 102 detectState(autoHidingNavbar); 103 } 104 105 function bindEvents(autoHidingNavbar) { 106 $document.on('scroll.' + pluginName, function() { 107 if (new Date().getTime() - _lastScrollHandlerRun > _throttleDelay) { 108 scrollHandler(autoHidingNavbar); 109 } 110 else { 111 clearTimeout(_scrollThrottleTimer); 112 _scrollThrottleTimer = setTimeout(function() { 113 scrollHandler(autoHidingNavbar); 114 }, _throttleDelay); 115 } 116 }); 117 118 $window.on('resize.' + pluginName, function() { 119 clearTimeout(_resizeThrottleTimer); 120 _resizeThrottleTimer = setTimeout(function() { 121 _windowHeight = $window.height(); 122 }, _throttleDelay); 123 }); 124 } 125 126 function unbindEvents() { 127 $document.off('.' + pluginName); 128 129 $window.off('.' + pluginName); 130 } 131 132 AutoHidingNavbar.prototype = { 133 init: function() { 134 this.elements = { 135 navbar: this.element 136 }; 137 138 this.setDisableAutohide(this.settings.disableAutohide); 139 this.setShowOnUpscroll(this.settings.showOnUpscroll); 140 this.setShowOnBottom(this.settings.showOnBottom); 141 this.setHideOffset(this.settings.hideOffset); 142 this.setAnimationDuration(this.settings.animationDuration); 143 144 _hideOffset = this.settings.hideOffset === 'auto' ? parseInt(this.element.css('height'), 10) : this.settings.hideOffset; 145 bindEvents(this); 146 147 return this.element; 148 }, 149 setDisableAutohide: function(value) { 150 this.settings.disableAutohide = value; 151 return this.element; 152 }, 153 setShowOnUpscroll: function(value) { 154 this.settings.showOnUpscroll = value; 155 return this.element; 156 }, 157 setShowOnBottom: function(value) { 158 this.settings.showOnBottom = value; 159 return this.element; 160 }, 161 setHideOffset: function(value) { 162 this.settings.hideOffset = value; 163 return this.element; 164 }, 165 setAnimationDuration: function(value) { 166 this.settings.animationDuration = value; 167 return this.element; 168 }, 169 show: function() { 170 show(this); 171 return this.element; 172 }, 173 hide: function() { 174 hide(this); 175 return this.element; 176 }, 177 destroy: function() { 178 unbindEvents(this); 179 show(this); 180 $.data(this, 'plugin_' + pluginName, null); 181 return this.element; 182 } 183 }; 184 185 $.fn[pluginName] = function(options) { 186 var args = arguments; 187 188 if (options === undefined || typeof options === 'object') { 189 return this.each(function() { 190 if (!$.data(this, 'plugin_' + pluginName)) { 191 $.data(this, 'plugin_' + pluginName, new AutoHidingNavbar(this, options)); 192 } 193 }); 194 } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') { 195 var returns; 196 197 this.each(function() { 198 var instance = $.data(this, 'plugin_' + pluginName); 199 200 if (instance instanceof AutoHidingNavbar && typeof instance[options] === 'function') { 201 returns = instance[options].apply(instance, Array.prototype.slice.call(args, 1)); 202 } 203 }); 204 205 return returns !== undefined ? returns : this; 206 } 207 208 }; 209 210})(jQuery, window, document);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.