Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yaroslavKas/db39c0531cafa2f73146cee11dd11736 to your computer and use it in GitHub Desktop.
Save yaroslavKas/db39c0531cafa2f73146cee11dd11736 to your computer and use it in GitHub Desktop.
МЕНЯЕМ АКТИВНЫЙ ПУНКТ МЕНЮ ПРИ ПРОКРУТКЕ СТРАНИЦЫ
var menu_selector = ".top-menu"; // Переменная должна содержать название класса или идентификатора, обертки нашего меню.
var menu = jQuery('#header').outerHeight();
jQuery("#mainnav li:first-child a").addClass("current");
function onScroll(){
var scroll_top = jQuery(document).scrollTop();
jQuery(menu_selector + " a").each(function(){
var hash = jQuery(this).attr("href");
var target = jQuery(hash);
if (target.position().top - menu <= scroll_top && target.position().top + target.outerHeight() > scroll_top) {
jQuery(menu_selector + " a.current").removeClass("current");
jQuery(this).addClass("current");
} else {
jQuery(this).removeClass("current");
}
});
}
jQuery(document).on("scroll", onScroll);
jQuery("#mainnav a[href^=#]").click(function(e){
e.preventDefault();
jQuery(document).off("scroll");
jQuery(menu_selector + " .current").removeClass("current");
jQuery(this).addClass("current");
var hash = jQuery(this).attr("href");
var target = jQuery(hash);
jQuery("html, body").animate({
scrollTop: target.offset().top
}, 300, function(){
window.location.hash = hash;
jQuery(document).on("scroll", onScroll);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment