Last active
December 15, 2015 23:59
-
-
Save b1nary/5343939 to your computer and use it in GitHub Desktop.
Scrollabe (jQuery anchor scroller for click & onload)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* (sexy) anchor scrolling for clicks and page loads | |
* Original author: Roman Pramberger @talkb1nary | |
* | |
* Licensed under the WTFPL license | |
* | |
* Example usage: | |
* $('body, html').scrollabe({ speed: 2000, listenLink: null }) | |
* | |
* Tested & working on: | |
* > Chrome >= 1 (???) | |
* > Firefox >= 3.6 (???) | |
* > IE >= 6 (???) | |
*/ | |
;(function ( $, window, document, undefined ) { | |
var pluginName = 'scrollabe', | |
defaults = { | |
speed: 1000, | |
easing: 'easeInOutQuad', | |
listenLink: 'a[href^="#"]' | |
}; | |
function Plugin( element, options ) { | |
this.element = element; | |
this.options = $.extend( {}, defaults, options) ; | |
this._defaults = defaults; | |
this._name = pluginName; | |
this.init(); | |
} | |
Plugin.prototype.init = function () { | |
if(window.location.hash != undefined){ | |
$(this.element).css('scrollTop', '0px'); // Gecko | |
var offs = $(window.location.hash).offset(); | |
if(offs != undefined){ | |
$(this.element).animate({ | |
'scrollTop': offs.top | |
}, this.options.speed, this.options.easing); | |
} | |
} | |
$(this.element).bind('scroll mousedown wheel DOMMouseScroll mousewheel keyup', function(e){ | |
if ( e.which > 0 || e.type == "mousedown" || e.type == "mousewheel"){ | |
$("html,body").stop(); | |
} | |
}) | |
if(this.options.listenLink != null){ | |
var that = this; | |
$(this.options.listenLink).on('click', function(e){ | |
e.preventDefault(); | |
$(that.element).animate({ | |
'scrollTop': $($(this).attr('href')).offset().top | |
}, that.options.speed, that.options.easing); | |
window.location.hash = $(this).attr('href'); | |
return false; | |
}); | |
} | |
}; | |
$.fn[pluginName] = function ( options ) { | |
return this.each(function () { | |
if (!$.data(this, 'plugin_' + pluginName)) { | |
$.data(this, 'plugin_' + pluginName, new Plugin( this, options )); | |
} | |
}); | |
} | |
})( jQuery, window, document ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment