Giter Club home page Giter Club logo

active-menu-link's Introduction

active-menu-link

NPM version NPM downloads

Highlight Active Menu Based On The Scroll Position

A pure JavaScript smooth scroll & scrollspy library which highlights the active menu item based on the scroll position.

A typical use of this library is to create a sticky header navigation for your single page app that allows the user to smoothly scroll through page sections while highlighting the active menu item.

Example

Example

Installation

yarn add active-menu-link
# OR
npm install active-menu-link

Usage

HTML

<nav class="navbar">
  <ul class="navbar-items">
    <li><a href="#first">First</a></li>
    <li><a href="#second">Second</a></li>
    <li><a href="#third">Third</a></li>
  </ul>
</nav>

<div class="blocks">
  <div class="block" id="first">First</div>
  <div class="block" id="second">Second</div>
  <div class="block" id="third">Third</div>
</div>

JavaScript

import ActiveMenuLink from "active-menu-link";

// Not necessary.
let options = {
  activeClass: "active"
};

new ActiveMenuLink(".navbar", options);

Options

Supported Options

Name Type Default Description
itemTag String li Selector to which the active class will be applied
activeClass String active Active class name
scrollOffset Number 0 Scroll offset
scrollDuration Number 500 Scroll duration in milliseconds
ease String out-circ Scroll animation (view ease for more)
headerHeight Number null Navbar height. If null, height calculate automatic
default String null If the scroll is not in one of the active sections, the default link will be active
showHash Boolean true Show hash in address bar

Default option example

HTML

<nav class="navbar">
  <ul class="navbar-items">
    <li><a href="#default">Default</a></li>
    <li><a href="#first">First</a></li>
    <li><a href="#second">Second</a></li>
    <li><a href="#third">Third</a></li>
  </ul>
</nav>

<div class="blocks">
  <div class="block" id="first">First</div>
  <div class="block" id="second">Second</div>
  <div class="block" id="third">Third</div>
</div>

JavaScript

import ActiveMenuLink from "active-menu-link";

let options = {
  default: "default"
};

new ActiveMenuLink(".navbar", options);

Building

git clone https://[email protected]/Kosalexei/active-menu-link.git
cd active-menu-link
npm install
npm run build #Compiled .js file go to the dist folder.

active-menu-link's People

Contributors

dependabot[bot] avatar kosalexei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

active-menu-link's Issues

itemTag requires nested a elements

According to this function:

private getLinks(): NodeList {
if (typeof this.menu === undefined) return null;
const links = this.menu.querySelectorAll(`${this.params.itemTag} a`);
return links;
}

Any parameter set for itemTag seems to look for all subelements that must be an a.
The way we have our nav structured is something like

<nav>
    <a id="logo" href="https://example.com">
        <img src="assets/logo.svg" alt="Our Logo" />
    </a>
    <a class="link" href="#general">GENERAL</a>
    <a class="link" href="#schedule">SCHEDULE</a>
    <a class="link" href="#faq">FAQ</a>
    <a class="link" href="#about">ABOUT US</a>
</nav>

As a feature suggestion, it'd be cool if itemTag was more of an itemQuery or something, so we could pass in, say,

itemTag: "a.link"

and leave out things like our logo, and other banners.

Awesome little library btw 😄

default: "default", not works

                                <li>
                                    <a href="#section_price">Цены</a>
                                </li>
    
                                <li>
                                    <a href="#section_reviews">Отзывы</a>
                                </li>
    
                                <li>
                                    <a href="https://ru.bem.info/methodology/css/">
                                        <img src="images/icons/lifebuoy.svg" alt="">
                                        Поддержка
                                    </a>
                                </li>
    
  			new ActiveMenuLink(".main_page_header_navigation",{

  			    itemTag: "li",
  			    activeClass: "active",

  			    // scrollOffset: document.getElementById("header").offsetHeight,
  			    scrollDuration: 900,

  			    ease: "linear",
  			    headerHeight: document.getElementById("header").offsetHeight,
  			    default: "default",
  			    showHash: false,
  			});

Direct links are a little sketch

When I try to link to a specific ID on our website, localhost:1234/#faq for example, it doesn't scroll to the right section on the first try of the link. If the page is reloaded it works fine though!

Problem occurs irregardless of what showHash is set to.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.