Jekyll - Automatically highlight current tab in menu bar

后端 未结 12 937
鱼传尺愫
鱼传尺愫 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:50

    I've been using page.path and going off the filename.

    <a href="/" class="{% if page.path == 'index.html' %}active{% endif %}">home</a>
    
    0 讨论(0)
  • 2020-12-22 15:53

    Yes you can do this.
    To accomplish this we will take advantage of the fact that the current page is always represented by the liquid variable: page in the template, and also that each post/page has a unique identifier in its page.url attribute.

    This means that we just have to use a loop to build our navigation page, and by doing so we can check page.url against every member of the loop. If it finds a match, it knows to highlight that element. Here we go:

      {% for node in site.pages %}
        {% if page.url == node.url %}
          <li class="active"><a href="{{node.url}}" class="active">{{node.title}}</a></li>
        {% else %}
          <li><a href="{{node.url}}">{{node.title}}</a></li>
        {% endif %}
      {% endfor %}
    

    This works as expected. However you probably don't want all your page's in your nav bar. In order to emulate page "grouping" you can something like this:

    ## Put the following code in a file in the _includes folder at: ./_includes/pages_list
    
    {% for node in pages_list %}
      {% if group == null or group == node.group %}
        {% if page.url == node.url %}
          <li class="active"><a href="{{node.url}}" class="active">{{node.title}}</a></li>
        {% else %}
          <li><a href="{{node.url}}">{{node.title}}</a></li>
        {% endif %}
      {% endif %}
    {% endfor %}
    {% assign pages_list = nil %}
    {% assign group = nil %}
    

    Now pages can be "grouped". To give a page a group you need to specify it in the pages YAML Front Matter:

    ---
    title: blah
    categories: blah
    group: "navigation"
    ---    
    

    Finally you can use your new code! Wherever you need your navigation to go in your template, simply "call" your include file and pass it some pages and the group you want to display:

    <ul>
      {% assign pages_list = site.pages %}
      {% assign group = 'navigation' %}
      {% include pages_list %}
    </ul>
    

    Examples

    This functionality is part of the Jekyll-Bootstrap framework. You can see exact documentation for the code outlined here: http://jekyllbootstrap.com/api/bootstrap-api.html#jbpages_list

    Finally you can see it in action within the website itself. Just look at the righthand navigation and you will see the current page is highlighted in green: Example highlighted nav link

    0 讨论(0)
  • 2020-12-22 15:56

    Lot of good answers are already there.

    Try this.

    I slightly alter the above answers.

    _data/navigation.yaml

    - name: Home
      url: /
      active: home
    - name: Portfolio
      url: /portfolio/
      active: portfolio
    - name: Blog
      url: /blog/
      active: blog
    

    In a page -> portfolio.html (Same for all pages with a relative active page name)

    ---
    layout: default
    title: Portfolio
    permalink: /portfolio/
    active: portfolio
    ---
    
    <div>
      <h2>Portfolio</h2>
    </div>
    

    Navigation html part

    <ul class="main-menu">
      {% for item in site.data.navigation %}
        <li class="main-menu-item">
          {% if {{page.active}} == {{item.active}} %}
            <a class="main-menu-link active" href="{{ item.url }}">{{ item.name }}</a>
          {% else %}
            <a class="main-menu-link" href="{{ item.url }}">{{ item.name }}</a>
          {% endif %}
        </li>
      {% endfor %}
    </ul>
    
    0 讨论(0)
  • 2020-12-22 15:56

    Here is a jQuery method to do the same

      var pathname = window.location.pathname;
    
      $(".menu.right a").each(function(index) {
        if (pathname === $(this).attr('href') ) {
          $(this).addClass("active");
        }
      });
    
    0 讨论(0)
  • 2020-12-22 15:58

    Lot's of confusing answers here. I simply use an if:

    {% if page.name == 'limbo-anim.md' %}active{% endif %} 
    

    I refer directly to the page and putting it inside the class I want to

    <li><a class="pr-1 {% if page.name == 'limbo-anim.md' %}activo{% endif %} " href="limbo-anim.html">Animación</a></li>
    

    Done. Quick.

    0 讨论(0)
  • 2020-12-22 15:59

    I feel like for the simplest navigation requirement, the listed solutions are overly complex. Here's a solution that involves no front matter, javascript, loops, etc.

    Since we have access to the page URL, we can normalize and split the URL and test against the segments, like so:

    {% assign current = page.url | downcase | split: '/' %}
    
    <nav>
      <ul>
        <li><a href='/about' {% if current[1] == 'about' %}class='current'{% endif %}>about</a></li>
        <li><a href='/blog' {% if current[1] == 'blog' %}class='current'{% endif %}>blog</a></li>
        <li><a href='/contact' {% if current[1] == 'contact' %}class='current'{% endif %}>contact</a></li>
      </ul>
    </nav>
    

    Of course, this is only useful if static segments provide the means to delineate the navigation. Anything more complicated, and you'll have to use front matter like @RobertKenny demonstrated.

    0 讨论(0)
提交回复
热议问题