Swipe List Menu in JavaScript

Swipe List Menu in JavaScript
Code Snippet:Pure JavaScript Swipe Menu
Author: Mohammad
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 4,500
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create a swipe list menu to reveal list options by swiping. It uses touchmove function to detect swipes and display options behind a list.

How to Create Swipe List Menu in JavaScript

1. First of all, create the HTML structure as follows:

<ul>
  <li>
    <div class="item">
      <svg viewBox="0 0 528.899 528.899">
      	 <path fill="white" d="M328.883,89.125l107.59,107.589l-272.34,272.34L56.604,361.465L328.883,89.125z M518.113,63.177l-47.981-47.981
      	 	c-18.543-18.543-48.653-18.543-67.259,0l-45.961,45.961l107.59,107.59l53.611-53.611
      	 	C532.495,100.753,532.495,77.559,518.113,63.177z M0.3,512.69c-1.958,8.812,5.998,16.708,14.811,14.565l119.891-29.069
      	 	L27.473,390.597L0.3,512.69z" />
      </svg>
      <svg viewBox="0 0 515.556 515.556">
        <path d="m64.444 451.111c0 35.526 28.902 64.444 64.444 64.444h257.778c35.542 0 64.444-28.918 64.444-64.444v-322.222h-386.666z" />
        <path d="m322.222 32.222v-32.222h-128.889v32.222h-161.111v64.444h451.111v-64.444z" />
      </svg>
      <div class="overlay">Swipe to Right & Left</div>
    </div>
  </li>
  <li>
    <div class="item">
      <svg viewBox="0 0 528.899 528.899">
      	 <path fill="white" d="M328.883,89.125l107.59,107.589l-272.34,272.34L56.604,361.465L328.883,89.125z M518.113,63.177l-47.981-47.981
      	 	c-18.543-18.543-48.653-18.543-67.259,0l-45.961,45.961l107.59,107.59l53.611-53.611
      	 	C532.495,100.753,532.495,77.559,518.113,63.177z M0.3,512.69c-1.958,8.812,5.998,16.708,14.811,14.565l119.891-29.069
      	 	L27.473,390.597L0.3,512.69z" />
      </svg>
      <svg viewBox="0 0 515.556 515.556">
        <path d="m64.444 451.111c0 35.526 28.902 64.444 64.444 64.444h257.778c35.542 0 64.444-28.918 64.444-64.444v-322.222h-386.666z" />
        <path d="m322.222 32.222v-32.222h-128.889v32.222h-161.111v64.444h451.111v-64.444z" />
      </svg>
      <div class="overlay">Swipe to Right & Left</div>
    </div>
  </li>
  <li>
    <div class="item">
      <svg viewBox="0 0 528.899 528.899">
      	 <path fill="white" d="M328.883,89.125l107.59,107.589l-272.34,272.34L56.604,361.465L328.883,89.125z M518.113,63.177l-47.981-47.981
      	 	c-18.543-18.543-48.653-18.543-67.259,0l-45.961,45.961l107.59,107.59l53.611-53.611
      	 	C532.495,100.753,532.495,77.559,518.113,63.177z M0.3,512.69c-1.958,8.812,5.998,16.708,14.811,14.565l119.891-29.069
      	 	L27.473,390.597L0.3,512.69z" />
      </svg>
      <svg viewBox="0 0 515.556 515.556">
        <path d="m64.444 451.111c0 35.526 28.902 64.444 64.444 64.444h257.778c35.542 0 64.444-28.918 64.444-64.444v-322.222h-386.666z" />
        <path d="m322.222 32.222v-32.222h-128.889v32.222h-161.111v64.444h451.111v-64.444z" />
      </svg>
      <div class="overlay">Swipe to Right & Left</div>
    </div>
  </li>
  <li>
    <div class="item">
      <svg viewBox="0 0 528.899 528.899">
      	 <path fill="white" d="M328.883,89.125l107.59,107.589l-272.34,272.34L56.604,361.465L328.883,89.125z M518.113,63.177l-47.981-47.981
      	 	c-18.543-18.543-48.653-18.543-67.259,0l-45.961,45.961l107.59,107.59l53.611-53.611
      	 	C532.495,100.753,532.495,77.559,518.113,63.177z M0.3,512.69c-1.958,8.812,5.998,16.708,14.811,14.565l119.891-29.069
      	 	L27.473,390.597L0.3,512.69z" />
      </svg>
      <svg viewBox="0 0 515.556 515.556">
        <path d="m64.444 451.111c0 35.526 28.902 64.444 64.444 64.444h257.778c35.542 0 64.444-28.918 64.444-64.444v-322.222h-386.666z" />
        <path d="m322.222 32.222v-32.222h-128.889v32.222h-161.111v64.444h451.111v-64.444z" />
      </svg>
      <div class="overlay">Swipe to Right & Left</div>
    </div>
  </li>
</ul>

2. After that, add the following CSS styles to your project:

body{
  font-family: 'Verdana';
  font-size: 14px;
}
main div{
  width: 300px;
  height: 40px;
  box-sizing: border-box;
  padding: 10px 15px;
  color: white;
  
}
ul{
  list-style: none;
  width: 300px;
  padding: 0px;
  margin: 0px auto;
}
ul li:not(:last-child){
  margin-bottom: 10px;
}
ul li .item{
  background: black;
  position: relative;
  overflow: hidden;
}
ul li .item svg{
  width: 15px;
  height: 15px;
}
ul li .item svg:nth-child(2){
  margin-left: 15px;
}
ul li .item svg path{
  fill: white;
}
ul li .item .overlay{
  position: absolute;
  top: 0px;
  left: 0px;
  background: #2f3d9a;
  transition: all 0.5s;
}

3. Finally, add the following JavaScript code and done.

var items = document.querySelectorAll('.overlay');

items.forEach(item => {
  item.addEventListener('touchstart', handleTouchStart, false);
  item.addEventListener('touchmove', handleTouchMove, false);
});

var xDown = null;
var yDown = null;

function getTouches(evt) {
  return evt.touches || evt.originalEvent.touches;
}

function handleTouchStart(evt) {
  const firstTouch = getTouches(evt)[0];
  xDown = firstTouch.clientX;
  yDown = firstTouch.clientY;
  elm = firstTouch.target;
};

function handleTouchMove(evt) {
  if ( ! xDown || ! yDown ) {
    return;
  }

  var xUp = evt.touches[0].clientX;
  var yUp = evt.touches[0].clientY;

  var xDiff = xDown - xUp;

  if ( xDiff > 0 ) {
    elm.style.left = '0px';
  } else {
    elm.style.left = '80px';
  }

  xDown = null;
  yDown = null;
};

That’s all! hopefully, you have successfully integrated this swipe list menu code snippet into your project. If you have any questions or facing any issues, 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