Skip to content

Instantly share code, notes, and snippets.

@larodiel
Created July 16, 2013 20:34
Show Gist options
  • Save larodiel/6012415 to your computer and use it in GitHub Desktop.
Save larodiel/6012415 to your computer and use it in GitHub Desktop.
function fixedTopMenu(topDistance, el, animationOptions, callback) {
var animationOptions = animationOptions || { duration:200, specialEasing: "easeOutBounce" };
var menuClone = "";
callback.onInit = callback.onInit || function() {};
callback.onShow = callback.onShow || function() {};
callback.onHide = callback.onHide || function() {};
if(animationOptions == "default")
{
animationOptions = { duration:200, specialEasing: "easeOutBounce" };
}
callback.onInit.call();
$(window).on("scroll", function(){
var currentTop = $(this).scrollTop();
if($(this).scrollTop() > topDistance)
{
if(menuClone == "")
{
menuClone = $(el).clone(true);
menuClone
.hide()
.appendTo("BODY");
}
menuClone
.addClass("fixed-header")
.fadeIn(animationOptions, callback.onShow.call());
}
else
{
if(typeof(menuClone) != "undefined" && menuClone != "" && menuClone.is(":visible"))
{
menuClone
.removeClass("fixed-header")
.fadeOut(animationOptions, callback.onHide.call());
}
}
});
}
/***
Usage
var showIn = 36;
fixedTopMenu(showIn, "#my-div-to-clone", {
onInit: function() {
//do something on init
},
onShow: function() {
//do something on show
},
onHide: function() {
//do something on hide
}
});
***/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment