joshwhatk
4/16/2015 - 2:20 PM

jwkSmoothScroll (AngularJS smooth scrolling service)

jwkSmoothScroll (AngularJS smooth scrolling service)

yourApp.factory('jwkSmoothScroll', function jwkSmoothScrollFactory()
{
		"use strict";
 
		function jwkSmoothScroll(input_hash, input_duration) {
			
			var hash = input_hash || $location.hash(),
			    duration = input_duration || 500,
			    element;
 
			// empty hash, scroll to the top of the page // no element and hash == 'top', scroll to the top of the page
			if (!hash || hash === 'top')
			{
				$('html, body').animate({
					scrollTop: $("html").offset().top
				}, duration);
			}
 
			// element with given id
			else if ((element = $('#' + hash)))
			{
				$('html, body').animate({
					scrollTop: element.offset().top
				}, duration);
			}
 
			// first anchor with given name :-D
			else if ((element = $('[name="'+ hash +'"]')))
			{
				$('html, body').animate({
					scrollTop: element.offset().top
				}, duration);
			}
		}
 
		return jwkSmoothScroll;
});