Create a Press and Hold Effect

Commonly seen in our phones, we have this gesture where you can press and hold to do something. In many apps, that gesture will simulate a right click. On the iOS and Android home screen, when you press and hold on an app icon, you will go into a mode that allows you to rearrange everything. There many other examples you can think of as well. Now, while this gesture might be popular on our phones, it certainly isn't just limited to them. You see it in various places on the web as well. For example, Medium has used this gesture forever:

In the above video, you can see that pressing and holding on the clap icon gives the author more upvotes depending on how long you have been pressing and holding.

In this tutorial, we'll learn the basic idea behind how to implement this press and hold gesture and look at how to create a simple example that takes advantage of it.

Onwards!

The Example

Before we go further, take a look at the following example that we will be building. To see it in action, press down on the circle with either your mouse or finger:

The longer you press down, the larger the circle gets until it gets to a maximum size. When you release the press, the circle animates back to its original state. In the following sections, we'll dissect how this gesture works so that you can more generally apply what you learn to a variety of situations.

General Press and Hold Basics

Ignoring any particular UI or final implementation for a moment, the way a press and hold gesture works is pretty simple:

You press down on some UI element using your mouse or finger

While the press is still active, you need to ensure you are holding the press

Once you've held the press long enough, you fire an event to signal that this gesture was successfully completed

When the press is released, you reset everything back to its original state

When we break our press and hold gesture into these steps, the work we need to do becomes pretty clear. To see these four steps in action, create a new HTML document and add the following content into it:

Once you have added all of this markup and code, save your HTML document and preview what you have in your browser. You will see a gray dotted circle appear. Bring up the Developer Tools, go to the Console tab, and press down on the circle without letting go. While doing this, you'll see all sorts of stuff show up on the Console:

What you are seeing is the output from each of the four steps that make our press and hold gesture work. Switch back to our code editor and take a few moments to understand what our code is doing. Once you feel like you've looked at the code for a bit, let's walk through it together.

It is Variable Time

The first things we see in our code are just some variable declarations:

Let's look at some of the important ones. First, the item variable refers to the UI element we will be pressing and holding on. In our example, it is the div element whose id value happens to also be item:

<div id="item"></div>

Next up is pressHoldDuration. This variable specifies how long you need to hold the press before this gesture is completed.

Lastly, there is one more variable declaration that you should be aware of:

var pressHoldEvent = new CustomEvent("pressHold");

We mentioned earlier that, after holding the press for a while, we fire an event to indicate that this gesture has completed. We define that event here, and it is called called pressHold. The thing to note is that this isn't a built-in DOM event. It is a custom event that, as you will see shortly, we fire ourselves.

Listening to Events

The next chunk of code revolves around the various events that are involved. To help us detect when we are pressing down or when we are no longer pressing down, we rely on the mousedown, mouseup, mouseleave, touchstart, and touchend events. The following code contains the addEventListener calls that associate these events with their corresponding event handler functions:

When we are pressing down via the mousedown and touchstart events, we have the pressingDown function that gets called. For when the press is released or cancelled, we have the notPressingDown function that gets called when the mouseup, mouseleave, and touchend events get fired. We'll look at both of those functions later.

Before we wrap this section on events up, the last event we have in this list is the following:

Remember the custom pressHold event we defined earlier? Well, we are listening to that event here and calling the doSomething event handling function when that event is fired.

Detecting the Hold

The most complicated part of our code has to do with figuring the hold part of this gesture. The hold is nothing more than a press that we don't let go of for a period of time. The way we technically implement this is as follows:

When we detect a press, we kick of a timer that increments a counter

The timer keeps ticking until the counter value reaches the limit specified by our pressHoldDuration value

When the limit is reached, we consider our hold requirement to be satisfied

The easiest way to see all of this mapped in our code is to skip down a bit and look at our timer function first:

Inside this function, the first thing we do is check whether our counter value has reached our pressHoldDuration value. If it hasn't, this means we are still in the middle of a press but haven't pressed down long enough. This is the code that runs during this case:

Our timer is powered by requestAnimationFrame. Because of how requestAnimationFrame works (see tutorial here), our timer function will be called about 60 times a second and our counter variable will be incremented by 1 each time. This code will keep executing until our counter < pressHoldDuration condition fails. When this condition fails, it means our counter value has reached the value of pressHoldDuration and the following code will execute instead:

To repeat what we said earlier, our pressingDown function gets called when the mousedown and touchstart events get fired. Inside this function, we just call our timer function once via requestAnimationFrame. Because this function is also called when our touch-oriented touchstart event is fired, we call preventDefault to avoid any unnecessary touch-related scrolling.

When we release our press, the notPressingDown function is called instead and further resets everything to its starting state. This function cancels the loop created by our requestAnimationFrame call by calling the cancelAnimationFrame method. The last thing it does is reset our counter variable back to 0.

Reacting to the touchHold Event

The last chunk of code we'll look at is also the easiest:

function doSomething(e) {
console.log("pressHold event fired!");
}

Our doSomething function is called when the touchHold event is overheard. We know this event is only fired when our gesture has successfully completed, so add any code here to react to this milestone. Pretty simple right?

Deconstructing our Press and Hold Example

Now that we've learned how to work with the press and hold gesture, let's revisit the example we started off with earlier. In our example, when you press and hold on the circle, the circle's size grows. When you release the hold, the circle's size resets to its original value.

There is nothing special about how this is made to work. All that is changing is the circle's scale transform value. There is a CSS transition in place to animate the scale value, so when it gets reset to its original size after releasing the hold, the resetting is smooth as opposed to sudden.

The following is the full markup and code for making this work, and I've highlighted the most important/interesting lines:

Notice that we are using a CSS variable called --scale-value to set the horizontal and vertical scale value in our transform property's scale3d function. There is a reason why this is done as opposed to just specifying the value in the scale3d function directly. By using a CSS variable, we can easily change the scale value in JavaScript. You can see how we do that by just using setProperty:

item.style.setProperty("--scale-value", 1 + counter / 50);

The alternative would be to to generate the full scale3d function as a string and replace the values inside it:

The CSS variable approach seems much cleaner, doesn't it? Anyway, as you can see from the highlighted lines, there isn't a whole lot else going on here. If you take a step back, we just took our generic press and hold starting point and extended it with a few lines of CSS and JS. Depending on what you are trying to do, your experience may be very similar in how little extra effort you need to put into writing new markup and code.

Conclusion

As we continue to blur the line between native apps and web apps, we need to meet users' high expectations on how the app should function. Emulating native gestures is one major part of this. In the past many sections, we dove deep into one really popular gesture - the press and hold. Are there other gestures that you'd like to see a future tutorial around how to implement using HTML, CSS, and JavaScript? If so, chime in in the comments below!

If you have a question about this or any other topic, the easiest thing is to drop by our forums where a bunch of the friendliest people you'll ever run into will be happy to help you out!