Skip to content

Instantly share code, notes, and snippets.

@kawabataryo
Last active August 29, 2015 14:08
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 kawabataryo/27812550e63bef056218 to your computer and use it in GitHub Desktop.
Save kawabataryo/27812550e63bef056218 to your computer and use it in GitHub Desktop.
一定量スクロールすると表示されるナビゲーション
/**
* @param {String} el 対象のセレクター ※必須
* @param {Number} position 特定の位置 ※必須
* @param {Number} height スライドの量 ※必須
* @param {Number} time スクロールするスピード [初期値=200]
* @param {String} easing イージング [初期値='swing']
*/
function NavControl(el,position,height,time,easing){
this.$el = $(el);
this.position = position;
this.height = height;
this.time = time || 200;
this.easing = easing || 'linear';
this.$window = $(window);
this.event();
}
NavControl.prototype = {
event: function(){
var that = this;
this.$window.on('scroll', function(){
var scrollTop = that.getScrollTop(this);
that.decidePosition(scrollTop);
});
},
getScrollTop: function(self){
return $(self).scrollTop();
},
decidePosition: function(scrollTop){
if( scrollTop > this.position ){
this.animation(0);
}else{
this.animation(this.height);
}
},
animation: function(n){
this.$el.stop().animate({top: n}, this.time, this.easing);
}
}
@kawabataryo
Copy link
Author

$(function(){
    //init
    new NavControl('selector', 136, -50);
    //('selector', display position, Quantity of the slide, time ,easing)
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment