bux23 of Micons
4/12/2017 - 10:26 AM

Smooth scroll to ID from link or url

Smooth scroll to ID from link or url

////////////////////////////////////////////////////////////////////////////////
// SMOOTH SCROLL TO ID
////////////////////////////////////////////////////////////////////////////////
jQuery(document).ready(function($){
	function scrollIt(divID) {
		$('html, body').animate({
	        scrollTop: $(divID).offset().top
	    }, 2000);
	}
	$(document).ready(function(e) {
		if(window.location.hash) {
		  var id = window.location.hash;
		  scrollIt(id);
		}
	});

	$(document).on('click','a', function(event) {
        if (!!$(this).attr('data-scroll')) {
            if(typeof $(this).attr('data-scroll') !== typeof undefined && $(this).attr('data-scroll') !== false) {
                if ($(this).attr('data-scroll') != '#' && $(this).attr('data-scroll').length > 3) {
                    event.preventDefault();
                    var id = "#" + $(this).data('scroll');
                    scrollIt(id);
                }
            } 
        } else if (!!$(this).attr('href')) {
			if(this.getAttribute("href").charAt(0) == "#") {
				event.preventDefault();
				if (this.getAttribute("href").length > 3) {
					var id = this.getAttribute("href");
					scrollIt(id);
				}
			}
		}
	});
});