How to Build a Vertical Off-Canvas Menu Bar Using JavaScript and CSS

on in JavaScript DOM
Last modified on

In my work, coming up with fresh navigation ideas and creating an intuitive and efficient navigation system is essential for enhancing user experience. One solution that stands out is the vertical off-canvas menu bar. This type of menu keeps navigation options hidden off-screen until needed, while showing a vertical bar only, which helps maintain a clean and uncluttered interface. It’s especially useful for mobile and responsive websites where screen space is at a premium.

In this tutorial, I’ll guide you through the process of creating a vertical off-canvas menu bar using JavaScript and CSS. We’ll cover each step in detail, ensuring you have all the information you need to implement this feature smoothly. Let’s get started and improve the navigation of your website.

For some of the elements, I have used the awesome Akar Icons pack.

HTML Structure

We’ll start by creating the HTML structure for our off-canvas menu. This will include a toggle button, the navigation menu, and some placeholder links. The structure is pretty simple and self-explanatory (I hope).

<link rel="stylesheet" href="https://unpkg.com/akar-icons-fonts/src/css/akar-icons.css">

<nav class="Navbar">
    <a href="#" id="toggle" class="Toggle Navbar-toggle" data-toggle="collapse" data-target="#navbarCollapse"><span></span></a>

    <div id="navbarCollapse" class="Navbar-menu">
        <ul class="Navbar-menu-major">
            <li><a href="#"><i class="ai-person"></i> My Account</a></li>
            <li><a href="#"><i class="ai-bell"></i> My Notifications</a></li>
            <li><a href="#"><i class="ai-chat-question"></i> My Support Requests</a></li>
            <li>
                <details>
                    <summary>
                        <i class="ai-airplay-audio"></i>
                        What are you looking for?
                        <i class="ai-chevron-vertical" style="font-size:smaller"></i>
                    </summary>

                    <p class="Navbar-meta">Tell us what you are looking for, so we can tailor the website to your needs.</p>
                    <p>
                        <select id="personaSelector" class="Navbar-persona-selector">
                            <option value="1">I need help with JavaScript</option>
                            <option value="2">I need help with PHP</option>
                            <option value="3">I need help with MySQL</option>
                            <option value="4">I need something else</option>
                        </select>
                    </p>
                </details>
            </li>
        </ul>

        <div class="Navbar-menu-minor">
            <ul>
                <li><a href="#"><i class="ai-shield"></i> Sample Page #1</a></li>
                <li><a href="#"><i class="ai-shield"></i> Sample Page #2</a></li>
            </ul>
        </div>
    </div>

    <ul class="Navbar-quickLinks">
        <li>
            <a href="#link" id="toggle-mini">
                <div class="sh">
                    <svg class="Navbar-shimmer">
                        <text dy="1em">What are you looking for?</text>
                    </svg>
                </div>
            </a>
        </li>
    </ul>
</nav>

<svg class="svg-def">
    <defs>
        <linearGradient id="svg-dark-shimmer" fy="0">
            <stop offset="0" stop-color="#999999" />
            <stop offset="0.3" stop-color="#999999" />
            <stop offset="0.6" stop-color="#ffffff" />
            <stop offset="0.7" stop-color="#999999" />
            <stop offset="1" stop-color="#999999" />
            <animateTransform attributeName="gradientTransform"
                            attributeType="XML"
                            type="translate"
                            from="-1.5 0"
                            to="1.5 0"
                            dur="2s"
                            repeatCount="indefinite"/>
        </linearGradient>
    </defs>
</svg>

CSS Styling

Next, we’ll style the off-canvas menu with CSS. This includes styling the menu, the toggle button, and the animation for opening and closing the menu. I will not explain what each CSS selector does, as you can use the Chrome/Firefox inspector if you need to change anything.

/* styles.css */

.Navbar-shimmer {
    height: 14px;
}

.sh {
    background: transparent;
    border: 0;
    padding: 0;
}

svg.svg-def {
    width: 0;
    height: 0;
    overflow: hidden;
    visibility: hidden;
}

.sh text { 
    fill: url(#svg-dark-shimmer); 
    width: 100%;  
}

.Navbar ul {
    list-style: none;
    padding-left: 0;
    margin: 0;
}

.Toggle {
    position: relative;
    display: inline-block;
    width: 5rem;
    height: 5rem;
    padding-top: 3rem;
    border-radius: 100%;
    background-color: transparent;
    border: 0;
    cursor: pointer;
    padding: 0;
}

.Toggle span {
    margin: -0.1rem auto 0;
    vertical-align: top;
    transition-duration: 0s;
    transition-delay: 0.2s;
    text-indent: 100%;
    line-height: 0;
    white-space: nowrap;
    overflow: hidden;
}

.Toggle span, 
.Toggle span:before, 
.Toggle span:after {
    display: block;
    width: 2.1rem;
    height: 0.2rem;
    background-color: #fff;
}

.Toggle span:before, 
.Toggle span:after {
    content: "";
    position: absolute;
    transition-property: margin, transform;
    transition-duration: 0.2s;
    transition-delay: 0.2s, 0s;
}

.Toggle span:before {
    margin-top: -0.7rem;
}

.Toggle span:after {
    margin-top: 0.7rem;
}

.Toggle:hover span:before {
    transform: translateY(-1px);
}

.Toggle:hover span:after {
    transform: translateY(1px);
}

.Toggle.is-active span {
    background-color: rgba(255, 255, 255, 0);
    transition-delay: 0.2s;
}

.Toggle.is-active span:before, 
.Toggle.is-active span:after {
    margin-top: 0;
    transition-delay: 0s, 0.2s;
}

.Toggle.is-active span:before {
    transform: rotate(45deg);
}

.Toggle.is-active span:after {
    transform: rotate(-45deg);
}

.Navbar {
    perspective: 800px;
    color: #fff;
    font-size: 14px;
    font-weight: 500;
    line-height: 1.8rem;
    position: fixed;
    z-index: 128;
    backface-visibility: hidden;
    top: 0;
    left: 0;
    bottom: 0;
    width: 5rem;
    background-color: #000;
}

.Navbar a:not(.Navbar-toggle) {
    color: inherit;
    text-decoration: none;
}

.Navbar-menu a,
.Navbar-quickLinks a {
    display: block;
}

.Navbar a i,
.Navbar details summary i {
    margin-right: 6px;
    opacity: 0.6;
}

.Navbar-quickLinks {
    width: 100vh;
    white-space: nowrap;
    position: absolute;
    bottom: 0;
    display: block;
    padding-left: 1.6rem !important;
    transform: rotate(-90deg) translateX(-6rem);
    transform-origin: 0 0;
}

.Navbar-quickLinks a {
    padding: 1.6rem 1.4rem;
}

.Navbar-quickLinks li {
    display: inline-block;
    width: 100%;
}

.Navbar-menu {
    line-height: 1.6rem;
    position: fixed;
    top: 0;
    left: 5rem;
    bottom: 0;
    display: flex;
    flex-direction: column;
    width: 26rem;
    transform: translateX(-100%);
    overflow-y: auto;
    visibility: hidden;
    padding: 2.4rem 0;
    padding-top: 5.2rem;
    background-color: rgba(0, 0, 0, 0.92);
    backdrop-filter: saturate(0);
    opacity: 0;
    transition-property: opacity, visibility, transform;
    transition-duration: 0.35s, 0.25s, 0.25s;
    transition-delay: 0.1s;
    transition-timing-function: cubic-bezier(0.22, 1, 0.36, 1);
}

.Navbar-menu .Navbar-menu-major {
    flex-grow: 1;
}

.Navbar-menu.is-active {
    opacity: 1;
    visibility: visible;
    width: 26rem;
    transform: translateX(0);
    border-left: 1px solid rgba(255, 255, 255, 0.2);
    transition-duration: 0.15s, 0.25s, 0.25s;
}

.Navbar-menu-major a,
.Navbar-menu-minor a,
.Navbar-menu-major details {
    padding: 1.6rem 10%;
}

.Navbar-menu-major details > summary {
    white-space: nowrap;
    list-style: none;
    cursor: pointer;
}

.Navbar-menu-major details > summary::-webkit-details-marker {
    display: none;
}

.Navbar-menu-major {
    font-size: 18px;
}

.Navbar-menu-major a:hover {
    background-color: rgba(255, 255, 255, 0.06);
}

.Navbar-menu-minor {
    color: #999999;
    margin-top: 1.4rem;
}

.Navbar-menu-minor a {
    padding-top: 0.7rem;
    padding-bottom: 0.7rem;
}

.Navbar-menu-minor a:hover {
    color: #fff;
}

.Navbar select {
    font-family: inherit;
    width: 100%;
    padding: 12px;
    background-color: black;
    color: white;
    border-radius: 3px;
    border: 1px solid rgb(255 255 255 / 0.4);
}

.Navbar select:hover {
    border: 1px solid rgb(255 255 255 / 0.6);
}

.Navbar .Navbar-meta {
    color: #999999;
    font-size: 14px;
    font-weight: 400;
}

.Navbar #toggle-mini {
    text-transform: uppercase;
    letter-spacing: 0.14em;
    font-size: 12px;
    font-weight: 700;
    color: #999999;
    background: linear-gradient(to right, #4d4d4d 0, #fff 10%, #4d4d4d 20%);
    background-position: 0;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    animation: shine 3s infinite linear;
    animation-fill-mode: forwards;
    -webkit-text-size-adjust: none;
}

@keyframes shine {
    0% {
        background-position: 0;
    }
    60% {
        background-position: 100%;
    }
    100% {
        background-position: 100%;
    }
}

.Navbar-toggle {
    transform: scale(.8);
    position: absolute;
    top: 0;
}

What I do want to explain, though, is the text shimmer effect, which is an “attention seeker”.

The SVG text shimmer effect is a visually appealing technique that gives text a shimmering or reflective look as if light is passing over it. This effect can add a touch of elegance and attention-grabbing motion to your web elements. Here’s a breakdown of how it works and how to implement it:

How the Effect Works

  1. SVG and Text Elements: The effect utilizes Scalable Vector Graphics (SVG) to render the text. SVG is perfect for this because it scales well across different screen sizes and resolutions.
  2. Linear Gradient: A linear gradient is used to create the shimmering effect. The gradient has multiple colour stops that transition smoothly from dark to light and back to dark.
  3. Animation: The shimmer effect is achieved by animating the gradient across the text. This gives the appearance of light moving across the surface of the text.

Implementing the SVG Text Shimmer Effect

Here’s a step-by-step guide to implementing the SVG text shimmer effect using HTML and CSS.

  1. Define the SVG and Linear Gradient: Start by defining an SVG element with a linear gradient in your HTML. This gradient will animate to create the shimmer effect.
<svg class="svg-def">
    <defs>
        <linearGradient id="svg-dark-shimmer" fy="0">
            <stop offset="0" stop-color="#999999" />
            <stop offset="0.3" stop-color="#999999" />
            <stop offset="0.6" stop-color="#ffffff" />
            <stop offset="0.7" stop-color="#999999" />
            <stop offset="1" stop-color="#999999" />
            <animateTransform attributeName="gradientTransform"
                              attributeType="XML"
                              type="translate"
                              from="-1.5 0"
                              to="1.5 0"
                              dur="2s"
                              repeatCount="indefinite"/>
        </linearGradient>
    </defs>
</svg>

The <linearGradient> element defines the gradient with multiple stops.

The <animateTransform> element animates the gradient, making it move from left to right repeatedly.

JavaScript Functionality

Finally, we’ll add JavaScript to handle the toggle functionality. This script will be responsible for opening and closing the off-canvas menu when the toggle button is clicked.

// script.js

document.getElementById("toggle").addEventListener("click", function() {
    this.classList.toggle("is-active");
    document.getElementById("navbarCollapse").classList.toggle("is-active");
});
document.getElementById("toggle-mini").addEventListener("click", function() {
    document.getElementById("toggle").classList.toggle("is-active");
    document.getElementById("navbarCollapse").classList.toggle("is-active");
});

Demo

See the Pen Untitled by Ciprian (@ciprian) on CodePen.

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *