Double-click to like Instagram effect in JavaScript ❤️


How to make that double click to like Instagram effect in JavaScript

3 Sep, 2020 · 3 min read

Today we are going to recreate the famous Instagram double-tap to like effect!

And the surprise, we only need 12 lines of JavaScript!

For this effect, we will bind a double-click handler to our image to show the heart animation plus the counter below the post.

This is how it is going to look:

Instagram like effect

HTML Structure

  style="position: absolute; width: 0; height: 0; overflow: hidden;"
    <symbol id="icon-heart" viewBox="0 0 32 32">
        d="M23.6 2c-3.363 0-6.258 2.736-7.599 5.594-1.342-2.858-4.237-5.594-7.601-5.594-4.637 0-8.4 3.764-8.4 8.401 0 9.433 9.516 11.906 16.001 21.232 6.13-9.268 15.999-12.1 15.999-21.232 0-4.637-3.763-8.401-8.4-8.401z"

<div class="post">
  <svg class="icon icon-heart">
    <use xlink:href="#icon-heart"></use>
  <p><span>0</span> likes</p>

As for our HTML, we use our SVG sprites. Then we create our post div, with the hearth SVG in it. Under that, we render the actual Instagram post. And last we have our number of likes.

As you can see, we are using singer elements, so I didn’t bother to add classes for this example.

CSS Structure

.post {
  margin: auto;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
.post img {
  width: 400px;
  cursor: pointer;
.post .icon {
  position: absolute;
  display: inline-block;
  width: 128px;
  opacity: 0;
  fill: red;
.post {
  animation: 2s like-heart-animation ease-in-out forwards;
.post p {
  align-self: baseline;
  margin-top: 10px;

We use Flex to center the post. Next, we make the icon (heart) absolute and give it an opacity of 0, so it’s not visible on load.

Then we say if this icon also has the class like add the animation like-heart-animation.

This animation looks like this, and it’s precisely the animation as to how Instagram does it. They scale it up and scale it a little down to give it a bounce effect.

@keyframes like-heart-animation {
  to {
    opacity: 0;
    -webkit-transform: scale(0);
    transform: scale(0);
  15% {
    opacity: 0.9;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  30% {
    -webkit-transform: scale(0.95);
    transform: scale(0.95);
  80% {
    opacity: 0.9;
    -webkit-transform: scale(1);
    transform: scale(1);

JavaScript Instagram Like

We need to get the like class added to our icon SVG for our actual effect to take place.

const img = document.querySelector('img');
const icon = document.querySelector('.icon');
const countEl = document.querySelector('span');
let count = 0;
img.addEventListener('dblclick', () => {
  countEl.innerHTML = count;
  setTimeout(() => {
  }, 1200);

We add an event listener called dblclick (double click). If that happens, we tell our counter to increase by one. Then we add the like class to our icon and update the number of likes. After 1200 milliseconds, we remove the like class again.

That’s it! We now have the following Instagram like effect in Codepen:

See the Pen Double-click to like Instagram effect in JavaScript by Chris Bongers (@rebelchris) on CodePen.

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

Removing all vowels with JavaScript

3 Dec, 2022 · 2 min read

Removing all vowels with JavaScript

10 games to learn JavaScript

2 Dec, 2022 · 3 min read

10 games to learn JavaScript

Join 2098 devs and subscribe to my newsletter