UNPKG

scroll-navigation-menu

Version:

A library to perform animated scrolling between sections and highlight the active section's <a> tag in the navigation menu

124 lines (110 loc) 2.54 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Anchor Navigation Menu demo</title> <style> html, body { padding: 0px; margin: 0px; } body { font-family: sans-serif; color: black; font-weight: bold; font-size: 2em; } nav { position: fixed; margin-bottom: 100px; width: 100%; height: 100px; display: flex; justify-content: center; align-items: center; background-color: lightgray; box-shadow: rgba(0, 0, 0, .2) 1px 1px; } nav.top { top: 0; left: 0; } nav.bottom { bottom: 0; left: 0; margin-bottom: 0px; } ul { width: 50%; display: flex; justify-content: space-between; padding: 0px; margin: 0px; } li { list-style-type: none; } li a { text-transform: uppercase; text-decoration: none; color: midnightblue; } li a.active { color: orange; } section { height: 500px; height: 100vh; } section#blue { background-color: mediumblue; } section#red { background-color: maroon; } section#green { background-color: lightseagreen; } @media(max-width: 768px) { body { font-size: 1em; } } </style> </head> <body> <nav class="top"> <ul> <li><a class="scroll" href="#top">top</a></li> <li><a class="scroll" href="#blue">blue</a></li> <li><a class="scroll" href="#red">red</a></li> <li><a class="scroll" href="#green">green</a></li> </ul> </nav> <div> <section id="top"></section> <section id="blue"></section> <section id="red"></section> <section id="green"></section> </div> <nav class="bottom"> <ul> <li><a class="scroll" href="#top">top</a></li> <li><a class="scroll" href="#blue">blue</a></li> <li><a class="scroll" href="#red">red</a></li> <li><a class="scroll" href="#green">green</a></li> </ul> </nav> <script> document.addEventListener('DOMContentLoaded', function() { const scrollNavigation = new ScrollNavigation({ activeClass: 'active', linksSelector: '.scroll', }); scrollNavigation.start(); }); </script> <script type="text/javascript" src="scroll-navigation-menu.js"></script></body> </html>