bux23
11/25/2016 - 9:05 PM

Smooth scroll to ID from link or url

Smooth scroll to ID from link or url

////////////////////////////////////////////////////////////////////////////////
// SMOOTH SCROLL TO ID
////////////////////////////////////////////////////////////////////////////////
var scrollIt = function(divID, speed) {
	if($(divID).length) {
		var elemTop = $(divID).offset().top;
		$('html, body').animate({
			scrollTop: elemTop
		}, 800).promise().then(function() {
			$(document).trigger('jquery_scrolling', {newPos: elemTop});
		});
	} else {
		console.log('trying to scroll to missing element ' + divID);
	}
}
$(window).load(function (e) {
    if (window.location.hash) {
        var id = window.location.hash;
        scrollIt(id);
    }
});
$(document).on('click', 'a[data-scroll]', function (event) {
    var target = $(this).attr('data-scroll');
    if (target != "") {
        event.preventDefault();
        scrollIt( "#" + target);
    }
});
$(document).on('click', 'a[href^="#"]', function (event) {
    var target = $(this).attr('href');
    if (target.length > 1) {
        event.preventDefault();
        scrollIt(target);
    }
});