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/9e2491a26e28bce6dfa7 to your computer and use it in GitHub Desktop.
Save kawabataryo/9e2491a26e28bce6dfa7 to your computer and use it in GitHub Desktop.
スムーズスクロール
/**
* @param {String} el 対象のセレクター ※必須
* @param {Number} sub スクロール位置を調整 [初期値=0]
* @param {Number} time スクロールするスピード [初期値=200]
* @param {String} easing イージング [初期値='swing']
*/
function PageScroll(el, sub, time, easing){
this.$el = $(el);
this.sub = sub || 0;
this.time = time || 200;
this.easing = easing || 'swing';
this.event();
}
PageScroll.prototype = {
event: function(){
var that = this;
this.$el.on('click', function(e){
e.preventDefault();
that.animation(this);
});
},
animation: function(self){
var $html = $('html,body');
$html.animate({
scrollTop: this.getPosition(self) - this.sub
}, this.time, this.easing);
},
getPosition: function(self){
var target = $(self).attr('href');
return $(target).offset().top;
}
}
@kawabataryo
Copy link
Author

//init
$(function(){
  new PageScroll('selector');//('selector', sub, time, easing)
});

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