Betiok
6/27/2017 - 12:58 PM

Smooth Scroll

Smooth Scroll

/**
* This part handles the highlighting functionality.
* We use the scroll functionality again, some array creation and
* manipulation, class adding and class removing, and conditional testing
*/

var aChildren = $('.collection-nav li').find('a'); // find the a children of the list items
var aArray = []; // create the empty aArray

for (var i = 0; i < aChildren.length; i++) {
    var aChild = aChildren[i];
    var ahref = $(aChild).attr('href');
    aArray.push(ahref);
} // this for loop fills the aArray with attribute href values

$(window).scroll(function(){
    var windowPos = $(window).scrollTop(); // get the offset of the window from the top of page
    var windowHeight = $(window).height(); // get the height of the window
    var docHeight = $(document).height();

    for (var i=0; i < aArray.length; i++) {
        var theID = aArray[i];
        var divPos = $(theID).offset().top - 105; // get the offset of the div from the top of page
        var divHeight = $(theID).height(); // get the height of the div in question
        if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
            $("a[href='" + theID + "']").addClass('current');
        } else {
            $("a[href='" + theID + "']").removeClass('current');
        }
    }

    if(windowPos + windowHeight == docHeight) {
        if (!$('.collection-nav li:last-child a').hasClass('current')) {
            var navActiveCurrent = $(".current").attr("href");
            $("a[href='" + navActiveCurrent + "']").removeClass('current');
            $('.collection-nav li:last-child a').addClass('current');
        }
    }
});
/* Smooth Scroll */

$('a[href*=#]:not([href=#])').click(function() {
	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') || location.hostname == this.hostname) {
		var target = $(this.hash);
		target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
		if (target.length) {
			$('html,body').animate({
				scrollTop: target.offset().top
		}, 1000);
			return false;
		}
	}
});