Изменить активный элемент меню при прокрутке страницы?


95

По мере прокрутки страницы вниз активный пункт меню изменяется. Как это сделать?

Ответы:


206

Это делается путем привязки к событию прокрутки контейнера (обычно окна).

Быстрый пример:

// Cache selectors
var topMenu = $("#top-menu"),
    topMenuHeight = topMenu.outerHeight()+15,
    // All list items
    menuItems = topMenu.find("a"),
    // Anchors corresponding to menu items
    scrollItems = menuItems.map(function(){
      var item = $($(this).attr("href"));
      if (item.length) { return item; }
    });

// Bind to scroll
$(window).scroll(function(){
   // Get container scroll position
   var fromTop = $(this).scrollTop()+topMenuHeight;

   // Get id of current scroll item
   var cur = scrollItems.map(function(){
     if ($(this).offset().top < fromTop)
       return this;
   });
   // Get the id of the current element
   cur = cur[cur.length-1];
   var id = cur && cur.length ? cur[0].id : "";
   // Set/remove active class
   menuItems
     .parent().removeClass("active")
     .end().filter("[href='#"+id+"']").parent().addClass("active");
});​

См. Приведенное выше действие в jsFiddle, включая анимацию прокрутки.


2
Если в вашем меню есть сочетание идентификаторов на странице и обычных страниц, сначала разместите ссылки для идентификаторов на странице, а затем измените их menuItems = topMenu.find("a"),на menuItems = topMenu.find("a").slice(0,4),, заменив 4на [ваши ссылки на странице - 1].
Стивен Сосье

5
На самом деле я использовал menuItems = topMenu.find ('a [href ^ = "#"]'), возвращая только якорные ссылки. Работает как шарм.
Julian K

1
Скрипка сломана. Не могли бы вы это исправить? Thx
m1crdy

1
@ m1crdy Спасибо за внимание. Это было исправлено. Похоже, что-то в jQuery edge сломалось. Прекрасно работает с 2.1.0 :)
mekwall

1
@JoelAzevedo Кажется, Sizzle изменилась. Обновлены ответ и тестовый пример для работы с jQuery 2.2.
mekwall

16

Просто проверьте мой код, снайперскую и демонстрационную ссылку:

    // Basice Code keep it 
    $(document).ready(function () {
        $(document).on("scroll", onScroll);

        //smoothscroll
        $('a[href^="#"]').on('click', function (e) {
            e.preventDefault();
            $(document).off("scroll");

            $('a').each(function () {
                $(this).removeClass('active');
            })
            $(this).addClass('active');

            var target = this.hash,
                menu = target;
            $target = $(target);
            $('html, body').stop().animate({
                'scrollTop': $target.offset().top+2
            }, 500, 'swing', function () {
                window.location.hash = target;
                $(document).on("scroll", onScroll);
            });
        });
    });

// Use Your Class or ID For Selection 

    function onScroll(event){
        var scrollPos = $(document).scrollTop();
        $('#menu-center a').each(function () {
            var currLink = $(this);
            var refElement = $(currLink.attr("href"));
            if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
                $('#menu-center ul li a').removeClass("active");
                currLink.addClass("active");
            }
            else{
                currLink.removeClass("active");
            }
        });
    }

демо жить


3

Просто чтобы дополнить ответ @Marcus Ekwall. Так вы получите только якорные ссылки. И у вас не возникнет проблем, если у вас будет сочетание якорных и обычных ссылок.

jQuery(document).ready(function(jQuery) {            
            var topMenu = jQuery("#top-menu"),
                offset = 40,
                topMenuHeight = topMenu.outerHeight()+offset,
                // All list items
                menuItems =  topMenu.find('a[href*="#"]'),
                // Anchors corresponding to menu items
                scrollItems = menuItems.map(function(){
                  var href = jQuery(this).attr("href"),
                  id = href.substring(href.indexOf('#')),
                  item = jQuery(id);
                  //console.log(item)
                  if (item.length) { return item; }
                });

            // so we can get a fancy scroll animation
            menuItems.click(function(e){
              var href = jQuery(this).attr("href"),
                id = href.substring(href.indexOf('#'));
                  offsetTop = href === "#" ? 0 : jQuery(id).offset().top-topMenuHeight+1;
              jQuery('html, body').stop().animate({ 
                  scrollTop: offsetTop
              }, 300);
              e.preventDefault();
            });

            // Bind to scroll
            jQuery(window).scroll(function(){
               // Get container scroll position
               var fromTop = jQuery(this).scrollTop()+topMenuHeight;

               // Get id of current scroll item
               var cur = scrollItems.map(function(){
                 if (jQuery(this).offset().top < fromTop)
                   return this;
               });

               // Get the id of the current element
               cur = cur[cur.length-1];
               var id = cur && cur.length ? cur[0].id : "";               

               menuItems.parent().removeClass("active");
               if(id){
                    menuItems.parent().end().filter("[href*='#"+id+"']").parent().addClass("active");
               }

            })
        })

В основном я заменил

menuItems = topMenu.find("a"),

по

menuItems =  topMenu.find('a[href*="#"]'),

Чтобы сопоставить все ссылки с привязкой где-нибудь, и изменить все, что было необходимо, чтобы заставить его работать с этим

Посмотрите в действии на jsfiddle


Как мне расширить это для вертикального меню, особенно когда меню больше, чем страница? pyze.com/product/docs/index.html Когда пользователь прокручивает контент справа, я хотел бы активировать соответствующее меню слева и прокрутить его, если необходимо, чтобы отобразить активное меню. Любые указатели приветствуются.
Дики Сингх

Это очень хорошо, спасибо. Однако я бы изменил селектор атрибутов с * = на ^ =. Если вы используете * =, вы также поймаете такие вещи, как google.com/#something, даже если это внешняя ссылка. Селектор атрибутов хорошо объяснен здесь: w3schools.com/css/css_attribute_selectors.asp
Жак,

0

Если вы хотите, чтобы принятый ответ работал в JQuery 3, измените код следующим образом:

var scrollItems = menuItems.map(function () {
    var id = $(this).attr("href");
    try {
        var item = $(id);
      if (item.length) {
        return item;
      }
    } catch {}
  });

Я также добавил try-catch, чтобы предотвратить сбой javascript, если с этим идентификатором нет элемента. Не стесняйтесь улучшать его еще больше;)

Используя наш сайт, вы подтверждаете, что прочитали и поняли нашу Политику в отношении файлов cookie и Политику конфиденциальности.
Licensed under cc by-sa 3.0 with attribution required.