Trovin
2/27/2019 - 2:06 PM

Check active section on sticky menu

Check active section on sticky menu

    //check active section
    function activeSection() {
        var flag = $('.page-header').hasClass('page-header_sticky');
        if(flag) {
            var links = $('.main-nav__link');
            links.each(function(key,value) {
                var newVal = $(value).attr('href');
                var headerHeight = parseInt($('.page-header').height());

                var currentSection = $(newVal);
                var scrollToElem = currentSection.offset().top;
                var scrollAfterElem = currentSection.offset().top + currentSection.height();
                var winScrollTop =  $(this).offset().top + headerHeight + 100;

                if(winScrollTop > scrollToElem && winScrollTop < scrollAfterElem) {
                    links.removeClass('main-nav__link_active');
                    var currentLink = links[key];
                    $(currentLink).addClass('main-nav__link_active');
                }
            });
        } else {
            $('.main-nav__link').removeClass('main-nav__link_active');
        }
    }
    activeSection();
    
    //on scroll
    $(window).on('scroll', function () {
        activeSection();
    });