Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var menu_selector = ".top-menu"; // Переменная должна содержать название класса или идентификатора, обертки нашего меню.
- function onScroll(){
- var scroll_top = $(document).scrollTop();
- $(menu_selector + " a").each(function(){
- var hash = $(this).attr("href");
- var target = $(hash);
- if (target.position().top <= scroll_top && target.position().top + target.outerHeight() > scroll_top) {
- $(menu_selector + " a.active").removeClass("active");
- $(this).addClass("active");
- } else {
- $(this).removeClass("active");
- }
- });
- }
- $(document).ready(function () {
- $(document).on("scroll", onScroll);
- $("a[href^=#]").click(function(e){
- e.preventDefault();
- $(document).off("scroll");
- $(menu_selector + " a.active").removeClass("active");
- $(this).addClass("active");
- var hash = $(this).attr("href");
- var target = $(hash);
- $("html, body").animate({
- scrollTop: target.offset().top
- }, 500, function(){
- window.location.hash = hash;
- $(document).on("scroll", onScroll);
- });
- });
- });
Add Comment
Please, Sign In to add comment