Double tap in JavaScript

Double tap in JavaScript

Introduction

The double tap like effect shows a beautiful heart animation when you tap twice on a photo. It’s a small but powerful UI feature that enhances user experience by providing instant feedback.

  • Fades out the animation after a short time.
  • Detects a double tap gesture.
  • Shows a heart or like icon animation.
double tap

Project overview

Core Features:

  • Detect double taps or double clicks
  • Display a heart icon dynamically
  • Add fade-in and fade-out animation
  • Support both desktop and mobile devices

Technologies Used:

  • HTML for structure
  • CSS for styling and animation
  • JavaScript for interactivity

Project Setup

Create a new folder named double tap and inside it, create the following files:

doble-tap/
│
├── index.html
├── style.css
└── script.js


Step-by-Step Implementation

HTML Layout

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" />
    <link rel="stylesheet" href="style.css" />
    <title>Double tap</title>
  </head>
  <body>
     <h3>Double tap on the image to <i class="fas fa-heart"></i> it</h3>
     <b><i><small>You liked it <span id="times">0</span> times</small></i></b>

      <div class="likeMe">

      </div>

       <script src="script.js"></script>
  </body>
</html>

CSS Styling

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');

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

body {
   font-family: 'Roboto', sans-serif;
   background-color: #4b515d;
   text-align: center;
   overflow: hidden;
   margin: 0;

}
/* #37474f */
h3 {
  color: white;
  margin-top: 4%;
  text-align: center;
}
 
small {
  color: white;
  display: block;
  padding-top: 8px;
  margin-bottom: 20px;
  text-align: center;
}

.fa-heart {
  color: red;
}

.likeMe {
  height: 440px;
  width: 300px;
  background: url('https://images.unsplash.com/photo-1589254065909-b7086229d08c?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=334&q=80') no-repeat center center/cover;
  margin: auto;
  cursor: pointer;
  max-width: 100%;
  position:relative;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.5), 0 10px 10px rgba(0, 0, 0, 0.5);
}

.likeMe .fa-heart {
  position: absolute;
  animation: grow 0.6s linear;
  transform: translate(-50%, -50%) scale(0);
}

@keyframes grow {
  to {
    transform: translate(-50%, -50%) scale(10);
    opacity: 0;
  }
}

JS Functionality

const likeMe = document.querySelector('.likeMe')
const times = document.querySelector('#times')

let clickTime = 0
let timesClicked = 0

likeMe.addEventListener('click', (e) => {
    if(clickTime === 0) {
        clickTime = new Date().getTime()
    } else {
        if((new Date().getTime() - clickTime) < 800) {
            createHeart(e)
            clickTime = 0
        } else {
            clickTime = new Date().getTime()
        }
    }
})

const createHeart = (e) => {
    const heart = document.createElement('i')
    heart.classList.add('fas')
    heart.classList.add('fa-heart')

    const x = e.clientX
    const y = e.clientY

   const leftOffset = e.target.offsetLeft
   const topOffset = e.target.offsetTop

    const xInside = x - leftOffset
    const yInside = y - topOffset

    heart.style.top = `${yInside}px`
    heart.style.left = `${xInside}px`

    likeMe.appendChild(heart)

    times.innerHTML = ++timesClicked

    setTimeout(() => heart.remove(), 1000)
}

How to use this project?

  • Download the project and extract the source code.
  • Open the folder on code editor or IDEs.
  • Launch the index file on the web browser.
  • Double tap to see the effect.
  • Enjoy and share!

In conclusion, creating a double-tap heart effect in JavaScript is a fun and practical way to learn about event handling, animations, and DOM manipulation. This project replicates Instagram’s like feature, giving users instant visual feedback and enhancing interactivity.

Although the source code is provided along with this project. Just copy and paste. Click the download button below to get the full source code for this project.

Build your own double-tap like effect today and make your web app feel just like Instagram!

Leave a Reply

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