Skip to content

Instantly share code, notes, and snippets.

@hachibeeDI
Created January 7, 2014 10:51
Show Gist options
  • Save hachibeeDI/8297730 to your computer and use it in GitHub Desktop.
Save hachibeeDI/8297730 to your computer and use it in GitHub Desktop.
押すと上にスルスルっと戻るJavaScriptモジュール ref: http://qiita.com/hatchinee/items/9e1ad803c4c57069ee39
var ns = (function(exports) {
var Raise = function constructor() {
this._init_handler = function(func) {
window.addEventListener('load', func);
};
};
Raise.prototype.register = function(id) {
var to_top = function(e) {
var scroll_top = document.documentElement.scrollTop || document.body.scrollTop;
if (scroll_top > 0) {
var diff = Math.max(scroll_top / 2, 20);
window.scrollTo(0, scroll_top - diff);
window.setTimeout(to_top, 25, e);
}
};
this._init_handler(function() {
var target = document.getElementById(id);
target.addEventListener('click', to_top);
});
};
exports.Raise = Raise;
return exports;
})({});
var RAISE_BUTTONS_ID = 'raise-top';
var raiser = new ns.Raise();
raiser.register(RAISE_BUTTONS_ID);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment