Демо:

https://codepen.io/ildar-meyker/pen/KKxqYvL

Плагины:

Подключение:

//

Разметка:

//

Стили:

//

Инициализация:

import throttle from "lodash/throttle";

let $sections = null;
let $scrollNav = null;

function handleWindowScroll() {
    console.log(1);
    const middleline = $(window).scrollTop() + $(window).height() / 2;

    const $needle = $sections
        .filter(function () {
            return $(this).offset().top <= middleline;
        })
        .last();

    if ($needle.length) {
        $scrollNav.find(".active").removeClass("active");
        $scrollNav.find(`a[href="#${$needle.attr("id")}"]`).addClass("active");
    }
}

$(function () {
    $sections = $('section[data-rel="scroll-nav"]');

    if ($sections.length === 0) return;

    $scrollNav = $(".js-scroll-nav");

    $(window).on("scroll", throttle(handleWindowScroll, 500));
});
Рубрики: модуль

0 комментариев

Добавить комментарий

Avatar placeholder

Ваш адрес email не будет опубликован. Обязательные поля помечены *