Vertical Tabs Using JavaScript

Vertical Tabs Using JavaScript
Code Snippet:Vertical Menu
Author: Lakshanaa Priya
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 1,447
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create a vertical tabs system that allows you to navigate between different sections of content. When you click on a tab, it displays relevant information in the content area. This method simplifies content organization and user interaction, making it helpful for creating organized, user-friendly web interfaces.

You can use this code in the website/app to implement vertical tab navigation. It helps improve user experience by keeping related content together.

How to Create Vertical Tabs Using JavaScript

1. First of all, load the Google Fonts by adding the following CDN links into the head tag of your webpage.

        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link
            href="https://fonts.googleapis.com/css2?family=Poppins&display=swap"
            rel="stylesheet"
            type='text/css'
        >

2. In your HTML file, create a container for your tabs and content. Each tab should have a unique ID and a corresponding content section. Here’s a simplified example:

<div class="tabs-container">
            <ul class="tabs">
                <li>
                    <a id="tab1" title="Analytics" href="#tab1">
                        <svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24"
                             viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" 
                             fill="none" stroke-linecap="round" stroke-linejoin="round"
                        >
                            <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                          <path d="M3 3v18h18"></path>
                            <path d="M20 18v3"></path>
                            <path d="M16 16v5"></path>
                            <path d="M12 13v8"></path>
                            <path d="M8 16v5"></path>
                            <path d="M3 11c6 0 5 -5 9 -5s3 5 9 5"></path>
                        </svg>
                        Analytics
                    </a>
                </li>
                <li>
                    <a id="tab2" title="Reports" href="#tab2">
                        <svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" 
                             viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"
                             fill="none" stroke-linecap="round" stroke-linejoin="round"
                        >
                            <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                            <path d="M7 7m-4 0a4 4 0 1 0 8 0a4 4 0 1 0 -8 0"></path>
                            <path d="M7 3v4h4"></path>
                            <path d="M9 17l0 4"></path>
                            <path d="M17 14l0 7"></path>
                            <path d="M13 13l0 8"></path>
                            <path d="M21 12l0 9"></path>
                        </svg>
                        Reports
                    </a>
                </li>

                <li>
                    <a id="tab3" title="Performance" href="#tab3">
                        <svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24"
                             height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"
                             fill="none" stroke-linecap="round" stroke-linejoin="round"
                        >
                            <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                            <path d="M3 12m0 1a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v6a1 1 0 0 1 -1 1h-4a1 1 0 0 1 -1 -1z"></path>
                            <path d="M9 8m0 1a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v10a1 1 0 0 1 -1 1h-4a1 1 0 0 1 -1 -1z"></path>
                            <path d="M15 4m0 1a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v14a1 1 0 0 1 -1 1h-4a1 1 0 0 1 -1 -1z"></path>
                            <path d="M4 20l14 0"></path>
                        </svg>
                        Performance
                    </a>
                </li>
                <li>
                    <a id="tab4" title="Funds" href="#tab4">
                        <svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24"
                             height="24" viewBox="0 0 24 24" stroke-width="2" 
                             stroke="currentColor" fill="none" stroke-linecap="round" 
                             stroke-linejoin="round"
                        >
                            <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                            <path d="M3 21l18 0"></path>
                            <path d="M3 10l18 0"></path>
                            <path d="M5 6l7 -3l7 3"></path>
                            <path d="M4 10l0 11"></path>
                            <path d="M20 10l0 11"></path>
                            <path d="M8 14l0 3"></path>
                            <path d="M12 14l0 3"></path>
                            <path d="M16 14l0 3"></path>
                        </svg>               
                        Funds
                    </a>
                </li>
            </ul>
            <div class="tab-content-wrapper">
                <section id="tab1-content" class="tab-content">
                    <h2>Analytics</h2>
                    <p>
                        Investment analytics involves the use of data, statistical techniques, and financial models to evaluate and optimize investment decisions. It's a discipline that aids investors in understanding potential risks and returns associated with various investment choices. Investment analytics can range from basic data analysis to complex quantitative models and can be applied to various asset classes.
                    </p>
                </section>
                <section id="tab2-content" class="tab-content">
                    <h2>Reports</h2>
                    <p>
                        Investment reports provide detailed information about the status, performance, and other relevant aspects of an investment or portfolio. They can be generated for individual investors or institutional investors and are typically used to assess the health, progress, and potential future trajectory of investments.
                    </p> 
                </section> 
                <section id="tab3-content" class="tab-content">
                    <h2>Performance</h2>
                    <p>
                        Investment performance refers to the returns generated on an investment relative to the amount of money invested. It's a way to measure and evaluate the growth or decline of an investment over a specific period, often compared to benchmarks or other standards to determine its relative success.
                    </p>
                </section>
                <section id="tab4-content" class="tab-content">
                    <h2>Funds</h2>
                    <p>
                        Investment funds are pooled collections of money from multiple investors that are managed collectively by professionals or on behalf of the investors. The main objective of these funds is to provide both individual and institutional investors access to a wider range of securities than they might be able to access individually. By doing so, they can achieve diversification, professional management, and economies of scale in investment.
                    </p>
                </section>
            </div>
        </div>

3. Customize the appearance of your tabs and content by adding CSS styles. Adjust colors, fonts, and dimensions to match your website’s design.

:root {
    --white: #FFF;
  --black
    --gray: #F3F3F3;
    --gray-mid: #9D9D9D;
    --gray-dark: #3e3e3e;
    --gray-hover: #F7F7F7;
    --body-background: grey;
}

body {
    font-family: 'Poppins';
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    padding: 16px;
    background-color: var(--body-background);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.tabs-container {
	padding: 16px;
    width: 100%;
    max-width: 480px;
    min-width: 320px;
    display: flex;
	margin: 10px;
	border-radius: 16px;
    background-color: var(--white);
	box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;	
}

header {
    position: relative;
}

.tab-content-wrapper {
    overflow-y: hidden;
    overflow-x: auto;
    height: 300px;
    transition: all 0.45s ease-in-out;
    padding: 0 16px;
}

.tab-content h2 {
    color: var(--gray-dark);
    font-size: 1.2rem;
    font-weight: 600;
    margin-top: 0px;
    margin-bottom: 8px;
}

.tab-content {
    font-size: 0.8rem;
    min-height: 300px;
    min-width: 240px;
    overflow-x: auto;
    transition: all 0.45s ease-in-out;
}

.tabs {
    position: relative;
    display: flex;
    flex-direction: column;
    border-right: 1px solid #F0F0F0;
    list-style-type: none;
    padding-right: 16px;
    gap: 8px;
}

.tabs a {
    position: relative;
    display: flex;
    text-decoration: none;
    width: 100%;
    font-size: 14px;
    font-weight: 600;
    color: var(--black);
    text-align: left;
    border-radius: 8px;
    justify-content: start;
    padding: 12px;
    align-items: center;
    gap: 8px;
    transition: all 0.3s ease-in-out;
}

.tabs a img {
    height: 20px;
    width: 20px;
}

.tabs .active, .tabs a:hover {
    font-weight: 700;
    outline: none;
    color: white;
    background-color: var(--gray-dark);
}


.tabs a svg {
    stroke: var(--black;
}

.tabs .active svg {
    stroke: var(--black);
}

.tabs a:hover svg {
    stroke: var(--black);
}

4. Finally, add the following JavaScript code to your project. This code handles tab switching and ensures the active tab is highlighted. Be sure to include it just before the closing </body> tag or within a script tag in your HTML.

const allLinks = document.querySelectorAll(".tabs a");
const allTabs = document.querySelectorAll(".tab-content")
const tabContentWrapper = document.querySelector(".tab-content-wrapper");

const shiftTabs = (linkId) => {
  allTabs.forEach((tab, i) => {
      
    if (tab.id.includes(linkId)) {
      allTabs.forEach((tabItem) => { 
        tabItem.style = `transform: translateY(-${i*300}px);`;
      });
    }
  });
}

allLinks.forEach((elem) => {
  elem.addEventListener('click', function() {
    const linkId = elem.id;
    const hrefLinkClick = elem.href;

    allLinks.forEach((link, i) => {
      if (link.href == hrefLinkClick){
        link.classList.add("active");
      } else {
        link.classList.remove('active');
      }
    });

    shiftTabs(linkId);
  });
});

//? handle proper selection for initial load
const currentHash = window.location.hash;

let activeLink = document.querySelector(`.tabs a`);

if (currentHash) {
  const visibleHash = document.getElementById(
    `${currentHash.replace('#', '')}`
  );

  if (visibleHash) {
    activeLink = visibleHash;
  }
}

activeLink.classList.toggle('active');

shiftTabs(activeLink.id);

That’s all! hopefully, you have successfully created Vertical Tabs Using Javascript. 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