Skip to content

Instantly share code, notes, and snippets.

@geckotang
Created July 23, 2013 02:08
Show Gist options
  • Save geckotang/6059330 to your computer and use it in GitHub Desktop.
Save geckotang/6059330 to your computer and use it in GitHub Desktop.
/**
* ある位置から固定する
* @param selector {String} 固定したい要素のセレクタ
* @param fixedClassName {String} 固定したい要素につけ外すclass名
*/
function fixedNav(selector, fixedClassName) {
var nav = $(selector || '#js-fixednav'),
offset = nav.offset(),
$window = $(window);
fixedClassName = fixedClassName || 'fixed';
$window.scroll(function () {
if($window.scrollTop() > offset.top - 20) {
nav.addClass(fixedClassName);
} else {
nav.removeClass(fixedClassName);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment