Bottom Navigation Bar with Indicator in JavaScript

Bottom Navigation Bar with Indicator in JavaScript
Code Snippet:Creating a Magic Bottom Menu with HTML, CSS, and JavaScript
Author: Ken deiparine
Published: March 3, 2024
Last Updated: March 4, 2024
Downloads: 1,969
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code creates a bottom navigation bar with indicator functionality. It uses event listeners to highlight active icons, providing visual feedback for navigation. The ‘moveLight’ function positions the indicator while ‘activeLink’ manages the active state of icons. This code helps build a user-friendly navigation experience.

You can use this code for web applications needing a sleek bottom navigation bar. Its benefit lies in enhancing user interaction by visually highlighting active icons during navigation.

How to Create Bottom Navigation Bar With Indicator In Javascript

1. First of all, load the BoxIcons CSS by adding the following CDN link into the head tag of your HTML document.

<link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>

2. Set up your HTML file. Include the necessary structure for the navigation bar.

<nav class="nav">
    <ul class="nav__links">
      <li class="nav__link active">
        <a href="#"><i class='bx bx-home-alt-2'></i></a>
      </li>
      <li class="nav__link">
        <a href="#"><i class='bx bx-heart' ></i></a>
      </li>
      <li class="nav__link">
        <a href="#"><i class='bx bx-plus-circle' ></i></a>
      </li>
      <li class="nav__link">
        <a href="#"><i class='bx bx-user' ></i></a>
      </li>
      <li class="nav__link">
        <a href="#"><i class='bx bx-bell' ></i></a>
      </li>

      <div class="nav__light"></div>
    </ul>
  </nav>

3. Create a CSS file (styles.css) and style your navigation bar elements to achieve the desired appearance. You can modify colors, sizes, and positioning to match your design.

/* ------------ VARIABLES ------------ */
:root{
  /* COLORS */
  --tab-color: #191919;
  --white-color: #fff;
  --home-icon-color: #00f7ff;
  --heart-icon-color: #ff0000;
  --plus-icon-color: #adff2f;
  --user-icon-color: #ee82ee;
  --bell-icon-color: #ffff00;
}

/* ------------ BASE ------------ */
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  list-style: none;
}

body{
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

li{
  display: inline-block;
}

/* ------------ MENU ------------ */
.nav{
  background-color: var(--tab-color);
  width: 30em;
  height: 8em;
  border-radius: 2em;
  padding: 0 2em;
  box-shadow: 0 1em 1em rgba(0,0,0, .2);

  display: flex;
  align-items: center;

  position: relative;
  overflow: hidden;
}

.nav__links{
  width: 100%;
  display: flex;
  justify-content: space-between;
}

.nav__link a{
  color: var(--white-color);
  font-size: 2.5rem;
  opacity: 0.5;
}

.nav__light{
  position: absolute;
  top: 0;
  left: 1.3em;
  background-color: var(--white-color);
  width: 4em;
  height: .4em;
  border-radius: 2px;

  display: flex;
  justify-content: center;

  transition: .3s ease;
}

.nav__light::before{
  content: '';
  width: 5em;
  height: 7em;
  position: absolute;
  top: .4em;
  background: linear-gradient(to bottom, rgba(255,255,255, .3) -50%, rgba(255,255,255, 0) 90%);
  clip-path: polygon(30% 0, 70% 0, 100% 100%, 0% 100%);
}


.nav__link.active a{
  opacity: 1;
}

.nav__link.active a .bx-home-alt-2{
  color: var(--home-icon-color);
  text-shadow: 0 0 15px var(--home-icon-color),
               0 0 30px var(--home-icon-color),
               0 0 45px var(--home-icon-color),
               0 0 60px var(--home-icon-color);
}

.nav__link:nth-child(1).active ~ .nav__light{
  background-color: var(--home-icon-color);
}


.nav__link.active a .bx-heart{
  color: var(--heart-icon-color);
  text-shadow: 0 0 15px var(--heart-icon-color),
               0 0 30px var(--heart-icon-color),
               0 0 45px var(--heart-icon-color),
               0 0 60px var(--heart-icon-color);
}

.nav__link:nth-child(2).active ~ .nav__light{
  background-color: var(--heart-icon-color);
}


.nav__link.active a .bx-plus-circle{
  color: var(--plus-icon-color);
  text-shadow: 0 0 15px var(--plus-icon-color),
               0 0 30px var(--plus-icon-color),
               0 0 45px var(--plus-icon-color),
               0 0 60px var(--plus-icon-color);
}

.nav__link:nth-child(3).active ~ .nav__light{
  background-color: var(--plus-icon-color);
}



.nav__link.active a .bx-user{
  color: var(--user-icon-color);
  text-shadow: 0 0 15px var(--user-icon-color),
               0 0 30px var(--user-icon-color),
               0 0 45px var(--user-icon-color),
               0 0 60px var(--user-icon-color);
}

.nav__link:nth-child(4).active ~ .nav__light{
  background-color: var(--user-icon-color);
}


.nav__link.active a .bx-bell{
  color: var(--bell-icon-color);
  text-shadow: 0 0 15px var(--bell-icon-color),
               0 0 30px var(--bell-icon-color),
               0 0 45px var(--bell-icon-color),
               0 0 60px var(--bell-icon-color);
}

.nav__link:nth-child(5).active ~ .nav__light{
  background-color: var(--bell-icon-color);
}

4. Finally, in a JavaScript file (script.js), implement the logic for the navigation bar functionality.

const links = document.querySelectorAll('.nav__link');
const light = document.querySelector('.nav__light');

function moveLight({offsetLeft, offsetWidth}){
  light.style.left = `${offsetLeft - offsetWidth/4}px`;
}

function activeLink(linkActive){
  links.forEach(link => {
    link.classList.remove('active');
    linkActive.classList.add('active');
  })
}


links.forEach((link) => {
  link.addEventListener('click', (event) => {
    moveLight(event.target);
    activeLink(link);
  })
})

If desired, you can customize the colors of the icons by adjusting the variables in the CSS ‘:root’ section. This step allows you to personalize the navigation bar to match your application’s theme.

That’s all! hopefully, you have successfully created a Bottom Navigation Bar With Indicator on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X