function createNavScrollSpy() {
var $slider = $(".ui-scrollspy");
function initScrollSpy() {
var headerHeight = $('#header').outerHeight() + 70;
var scrollTop = $(document).scrollTop() + headerHeight;
if($slider.hasClass('initialized')) {
return;
}
$slider.find('li').each(function(){
var hash = $(this).find('a').attr("href");
var $target = $(hash);
if($target.length == 0) {
$(this).remove();
return;
}
if ($target.offset().top <= scrollTop + $(window).height() / 2 - $target.outerHeight() / 2) {
$slider.find("li.active").removeClass("active");
$(this).addClass("active");
} else {
$(this).removeClass("active");
}
});
}
if($slider.length) {
initScrollSpy();
$slider.find('a').click(function(e){
e.preventDefault();
$(document).off("scroll", initScrollSpy);
$slider.find("li.active").removeClass("active");
$(this).closest('li').addClass("active");
var hash = $(this).attr("href");
var target = $(hash);
var offsetTop = target.offset().top - $(window).height() / 2 + target.outerHeight() / 2;
$("html, body").animate({
scrollTop: offsetTop
}, 500, function(){
//window.location.hash = hash;
$(document).on("scroll", initScrollSpy);
}
);
});
$(document).on("scroll", initScrollSpy);
}
}