Skip to content

Instantly share code, notes, and snippets.

@dtomasi
Last active August 31, 2018 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtomasi/175c8bc2cc7fd45f20ee to your computer and use it in GitHub Desktop.
Save dtomasi/175c8bc2cc7fd45f20ee to your computer and use it in GitHub Desktop.
StickNav pins navigation-bars to the top if scrolling over it.
/**
* jQuery Plugin StickyNav
* @copyright tomasiMEDIA 2013
* @author Dominik Tomasi
* @date 09.10.13
*/
(function ($) {
$.StickyNav = function (el, options) {
var base = this;
base.defaultOptions = {
placeholder: false,
css: {
'position': 'fixed',
'top': 0,
'z-index': 999
}
};
base.$el = $(el);
base.init = function () {
base.options = $.extend({}, base.defaultOptions, options);
base.offset = base.$el.offset().top;
base.originHeight = base.$el.height();
var placeholder = $('<div class="placeholder"></div>');
placeholder.css('height', base.originHeight);
$(window).scroll(function () {
var totalScroll = $(window).scrollTop();
if (totalScroll >= base.offset) {
//Change CSS-Propertys
base.$el.css(base.options.css);
//if Placeholder -> insert him
if (base.options.placeholder) {
placeholder.insertBefore(base.$el);
}
}
if (totalScroll <= base.offset) {
if (base.options.placeholder) {
placeholder.remove();
}
base.$el.removeAttr('style');
}
});
};
base.init();
};
window.StickyNav = function (selector, options) {
(new $.StickyNav(selector, options))
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment