Detecting the "swap" to close a menu with CSS

Think about your users when they visit your website on their mobile devices: allowing them to close your menu using the "swap" will make their experience on your site much easier, so you'll have a better user experience, which increases your chances of building loyalty. Let's find out how in this article!
•
Updated on August 14, 2026
•
2 min read
Believemy logo

Think about your users when they visit your website on their mobile devices: allowing them to close your menu using a "swipe" will greatly enhance their experience on your site, providing a better user experience and increasing your chances of retaining them. Let’s discover together how to do this in this article!

Image

Step 1

Let's start with the HTML code.

HTML
<div class="hamburger">
  <span></span>
  <span></span>
  <span></span>
</div>

<div class="sidebar"></div>

The tags with the hamburger class will allow us to style the lines that make up the icon.

Step 2

Let’s handle the hamburger class.

CSS
.hamburger {
  position: absolute;
  top: 50px;
  left: 35px;
  cursor: pointer;
}

.hamburger span {
  display: block;
  width: 30px;
  height: 0.3rem;
  margin: 0.3rem 0;
  background-color: #e91e63;
  border-radius: 5px;
}

Step 3

Now it's our menu's turn!

CSS
.sidebar {
  position: absolute;
  top: 0;
  left: 0;
  width: 210px;
  height: 100vh;
  background-color: #e91e63;
  transform: translateX(-210px);
  transition: all .2s;
}

.sidebar.open {
  transform: translateX(0);
}

Step 4

Let's detect the click on our icon using JavaScript.

JAVASCRIPT
const icon = document.querySelector('.hamburger');
const sidebar = document.querySelector('.sidebar');

let touchStart, touchEnd;

icon.addEventListener('click', () => {
  sidebar.classList.add('open');
});

Step 5

All that's left is to capture the user's swipe movement.

JAVASCRIPT
sidebar.addEventListener('touchmove', e => {
  touchEnd = e.targetTouches[0].clientX;
});

sidebar.addEventListener('touchend', e => {
  if (touchStart - touchEnd > 100) {
    sidebar.classList.remove('open');
  }
});

Using the touchstart and touchmove events, we store the distance moved by the user's finger.

Step 6

Let's calculate the swipe distance to finalize the functionality.

JAVASCRIPT
sidebar.addEventListener('touchend', e => {
  if (touchStart - touchEnd > 100) {
    sidebar.classList.remove('open');
  }
});

If our user has swiped more than 100px in distance, we remove the open class from our menu to close it.

Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

Create a menu that spans the entire page using CSS

From time to time we have to ask our visitors which page they want to go to, by displaying the main sections of our website. This menu is often not very pleasant to use, so let's find out together in this article how to make it much more modern, with beautiful animations that will make your passers-by want to visit your site!

December 9, 2024

Create a full-screen menu on click

Having a menu has become an indispensable classic. But having a menu that takes up the whole screen is ideal when you want your site to be totally responsive (and therefore suitable for mobile devices, for example). This article will help you to set up a full-screen menu that is activated on click!

December 9, 2024

Make an arrow that expands when hovered over with CSS

Whether it's to make your users want to see your project, read your article or access a page on your website, presenting your link with an arrow will make it easier to catch your visitors' eye! Let's find out in this article how to animate this arrow to enhance your readers' experience.

December 9, 2024

Make an animated notification message with JavaScript

Whether it's to quickly warn your users that a feature of your site is not yet available or to let them know that an action has been successfully completed, notifications are the simplest and most modern way to set up. Find out how in this article!

December 9, 2024

Detect the preference (DARK | LIGHT) of a user's computer

Offering a dark version of your website is gradually becoming a standard that sites need to adopt for the benefit of their users. In this article we'll look at how to create a dark theme and how to detect your visitor's preferences.

December 9, 2024

Creating circled buttons on hover with HTML and CSS

For a menu or to encourage your readers to share your website, share buttons are essential elements to add to your website. Let's find out how to make them more modern with a "neon" effect that will surround the icon selected by your user.

December 9, 2024

Create hover text on image with CSS

We've already seen how to create an element that is displayed when the mouse hovers over an image, but in this article we'll show you how to display a whole host of information very fluidly by hovering the mouse over an element!

December 9, 2024

Creating an animated list with CSS

Let's find out how to create an animated hover list!

December 9, 2024

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.