Draggable Circular Navigation Menu Using HTML, CSS & JavaScript for Modern Web UI

Creating intuitive and interactive navigation menus is a key element of modern web design. Among the many creative approaches to user interface (UI) design, a draggable circular navigation menu stands out for both its aesthetic appeal and its practical functionality. This article dives deep into how to build a draggable circular navigation menu using HTML, CSS, and JavaScript. We will explore the logic behind the layout, styling techniques for a modern interface, and JavaScript's role in interaction and motion handling. The final result is a smooth, dynamic, and user-friendly navigation menu that can be reused or extended in various web applications.

Draggable Circular Navigation Menu Using HTML, CSS & JavaScript for Modern Web UI

Overview of Circular Navigation

Circular navigation is a non-linear user interface element where menu options are arranged in a circular pattern around a central trigger or button. Unlike traditional sidebars or top navigation bars, circular navigation allows you to pack multiple links or actions around a single icon, minimizing screen real estate while providing a highly interactive experience.

This kind of UI is often used in creative portfolios, mobile apps, and experimental websites where aesthetics and interactivity play a significant role. What makes it even more dynamic is the ability to drag the menu around the screen - this provides flexibility and enhances user engagement.

Why Use a Draggable Interface?

Draggable interfaces bring a layer of personalization and freedom to the user experience. When users can reposition a UI element, it adds a feeling of control and adaptability. For users working on large screens or multitasking on desktops, the ability to move UI components is not just fancy - it’s functional. A draggable circular menu gives your web app a distinctive UX flavor while maintaining a clean and uncluttered design.

HTML Structure

Let’s start by examining the basic HTML structure. This part of the code defines the navigation container, toggle button, and individual menu icons. Each icon is housed inside a <span> element and styled using Boxicons.

<nav class="floating-nav">
  <div class="nav-wrapper">
    <div class="menu-toggle">
      <i class='bx bx-plus'></i>
    </div>
    <span style="--i:1;">
      <a href="#"><i class='bx bx-home-alt-2'></i></a>
    </span>
    <span style="--i:2;">
      <a href="#"><i class='bx bx-photo-album'></i></a>
    </span>
    <span style="--i:3;">
      <a href="#"><i class='bx bx-time-five'></i></a>
    </span>
    <span style="--i:4;">
      <a href="#"><i class='bx bx-map-pin'></i></a>
    </span>
    <span style="--i:5;">
      <a href="#"><i class='bx bx-slider-alt'></i></a>
    </span>
  </div>
</nav>

Styling the Interface with CSS

The CSS portion is responsible for the circular layout, positioning, animations, and draggable cursor styling. We use transform: rotate() and translateY() to position the items in a circular shape. The transitions give it a smooth unfolding animation when toggled.

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

body {
  height: 100vh;
  background: #17a2b8;
  overflow: hidden;
}

.floating-nav {
  position: absolute;
  top: 20px;
  right: 0;
  width: 80px;
  height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
}

.floating-nav .nav-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  transform: rotate(-45deg);
}

.nav-wrapper .menu-toggle,
.nav-wrapper span a {
  height: 60px;
  width: 60px;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
}

.nav-wrapper .menu-toggle {
  font-size: 35px;
  color: #0e2431;
  z-index: 100;
  cursor: pointer;
  transform: rotate(-225deg);
  transition: all 0.6s ease;
}

.floating-nav.open .menu-toggle {
  transform: rotate(0deg);
}

.nav-wrapper span {
  position: absolute;
  transition: all 0.6s ease;
  opacity: 0;
}

.floating-nav.open .nav-wrapper span {
  transform: rotate(calc(var(--i) * (360deg/8))) translateY(120px);
  opacity: 1;
}

.nav-wrapper span a {
  text-decoration: none;
  transform: rotate(45deg);
}

.nav-wrapper span a i {
  font-size: 24px;
  color: #0e2431;
  transform: rotate(calc(var(--i) * (360deg/ -8)));
  opacity: 0.8;
  transition: 0.2s;
}

.nav-wrapper span a:hover i {
  opacity: 1;
}
</style>

JavaScript Interactivity

JavaScript plays two essential roles here: it toggles the menu’s open/close state and makes the navigation menu draggable across the screen. This combination of functionality adds both interactivity and freedom for the user.

<script>
const nav = document.querySelector(".floating-nav"),
      toggleBtn = nav.querySelector(".menu-toggle");

toggleBtn.addEventListener("click", () => {
  nav.classList.toggle("open");
});

function onDrag({ movementY }) {
  const navStyle = window.getComputedStyle(nav),
        navTop = parseInt(navStyle.top),
        navHeight = parseInt(navStyle.height),
        windHeight = window.innerHeight;

  nav.style.top = navTop > 0 ? `${navTop + movementY}px` : "1px";
  if (navTop > windHeight - navHeight) {
    nav.style.top = `${windHeight - navHeight}px`;
  }
}

nav.addEventListener("mousedown", () => {
  nav.addEventListener("mousemove", onDrag);
});

nav.addEventListener("mouseup", () => {
  nav.removeEventListener("mousemove", onDrag);
});

nav.addEventListener("mouseleave", () => {
  nav.removeEventListener("mousemove", onDrag);
});
</script>

Enhancing User Experience

  • Adding touch support for mobile users using touchstart and touchmove.
  • Storing the position of the menu using localStorage.
  • Allowing horizontal dragging with a more complex handler.
  • Integrating it with a content management system or React for a component-based approach.

Accessibility Considerations

Ensure to include aria-labels for icons, keyboard accessibility for toggle, and alternative navigation for screen readers. Currently, this menu is visually stunning but may not meet accessibility standards without enhancements.


Demo

Please click the button below to view the demo.


Final Thoughts and Use Cases

Draggable circular navigation menus offer an interactive and innovative experience for modern web applications. They are particularly suitable for creative portfolios, dashboards, mobile-like web interfaces, and interactive product tours. The combination of CSS transforms and JavaScript event handling enables a responsive, touch-friendly, and flexible design that captivates users.

This pattern goes beyond aesthetics - when implemented well, it contributes to a site’s usability, especially when space is a constraint or a non-linear navigation pattern is desired.

As a frontend developer or designer, integrating these patterns into your workflow demonstrates your ability to think outside traditional layouts and to bring joyful experiences to the users you build for.

Previous Post

Cookies Consent

This website uses cookies to analyze traffic and offer you a better Browsing Experience. By using our website.

Learn More