Jekyll - Automatically highlight current tab in menu bar

后端 未结 12 1006
鱼传尺愫
鱼传尺愫 2020-12-22 15:34

I am using github to host a static site and Jekyll to generate it.

I have a menu bar (as

    ) and would like the
  • correspo
12条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-22 15:41

    I used a little bit of JavaScript to accomplish this. I have the following structure in the menu:

    
    

    This javascript snippet highlights the current corresponding page:

    $(document).ready(function() {
        var pathname = window.location.pathname;
    
        $("#navlist a").each(function(index) {
            if (pathname.toUpperCase().indexOf($(this).text().toUpperCase()) != -1)
                $(this).addClass("current");
        });
    
        if ($("a.current").length == 0)
            $("a#index").addClass("current");
    });
    

提交回复
热议问题