JQuery + Bootstrap navigation bar - how to highlight a link when visiting

Please visit the link.

Try clicking the "Portfolio" link, it should be highlighted in blue.

But after clicking on the navigation bar, it will be highlighted in blue.

    //scrolling
$('nav a').on('click', function() {

    var scrollAnchor = $(this).attr('data-scroll'),
        scrollPoint = $('section[data-anchor="' + scrollAnchor + '"]').offset().top;

    $('body,html').animate({
        scrollTop: scrollPoint
    }, 500);

    return false;

});

//change color
$(window).scroll(function() {
    var windscroll = $(window).scrollTop();
    if (windscroll) {
        $('section').each(function(i) {
            if ($(this).position().top <= windscroll) {
                $('nav a.active').removeClass('active');
                $('nav a').eq(i).addClass('active');
            }
        });

    }
}).scroll();
+4
source share
2 answers

try it

nav a.active,
nav a.active:hover {
  background-color: blue !important;
}
0
source

Just like hover, you can assign a color to visted links.

a:visited {
    color: blue;
}
0
source

Source: https://habr.com/ru/post/1673565/


All Articles