The application described in this guide shows how to use touch events for simple single and multi-touch interactions, the basics needed to build application-specific gestures. A live version of this application is available on Github. The source code is available on Github and pull requests and bug reports are welcome. The application uses is the generic container for flow content.

It has no effect on the content or layout until styled using CSS. Event handlers are registered for all four touch event types. The touchend and touchcancel event types use the same handler.

double touch event javascript

The code does not include error handling, or vertical moving. Note that the threshold for pinch and zoom movement detection is application specific and device dependent. The touchstart event handler caches touch points to support 2-touch gestures. The touchend handler restores the event target's background color back to its original color. The background color of the touch areas will change as follows: no touch is white ; one touch is yellow ; two simultaneous touches is pinkand three or more simultaneous touches is lightblue.

The functions are used to log event activity to the application window, to support debugging and learning about the event flow. Get the latest and greatest from MDN delivered straight to your inbox. Sign in to enjoy the benefits of an MDN account. Multi-touch interaction. Define touch targets The application uses is the generic container for flow content. Update background color The background color of the touch areas will change as follows: no touch is white ; one touch is yellow ; two simultaneous touches is pinkand three or more simultaneous touches is lightblue.

Related Topics. Learn the best of web development Get the latest and greatest from MDN delivered straight to your inbox. The newsletter is offered in English only at the moment. Sign up now.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

The touchend event is triggered when the touch ends. If the default action is prevented, a click event will not trigger.

This is not my code but I can't remember where I got it from, used successfully. It uses jQuery but no extra libraries or plugins for the tap handling itself. I used this for a project exclusively for iPad, so might need tweaking to work for desktop and tablet together.

There are touchstarttouchend and other events. You can add event listeners for them in this way:. I wrote a little script myself. It's not in pure-JS, but works fine for me. It prevents executing the script on scrolling, meaning the script only fires on a 'tap'-event. Learn more. Asked 7 years, 4 months ago. Active 2 years, 4 months ago. Viewed 50k times.

How can I capture a user's "tap" event with pure JS? I cannot use any libraries, unfortunately. Chuck Le Butt Active Oldest Votes. The click event is triggered on mouse click as well as on a touch click. The touchstart event is triggered when the screen is touched. John Dvorak John Dvorak DineshKumarDJ feel free to post some example code as a separate answer.

A comment to another answer isn't a good place to post it because people then can't judge the quality of your suggestion. Kayo Kayo 3 3 silver badges 10 10 bronze badges.

This isn't pure JS as you point out, so it doesn't answer the question. NieDzejkob, it's not a useful answer to this question. It's easy enough to find solutions with jQuery or other libraries, but sometimes a dev is restricted and cannot use a library, which is why the OP specified "pure JavaScript". If you want to point at a jQuery answer in a comment, as Dinesh does above, that is fine.

HD-it HD-it 71 2 2 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook.In this tutorial lets get touchy feely with JavaScript, by examining its touch related events and how they are used to detect and respond to touch and swipe events. With touch based devices ever growing in numbers, grasping these events is as essential as understanding the age old mouse events.

Subscribe to RSS

Examples in this tutorial can be appreciated in both touch and non-touch enabled devices, with the later falling back to your trusted mouse instead. Ready to give those fingers a bit of a workout? Lets go! So lets dive right into it. The following lists the supported touch events in JavaScript:. These events can be attached to any element on the page, and is passed an event object containing details about the touch point, such as its coordinates on the page.

Use element. Here I've attached the " touchstart " event to document. Inside the anonymous function for touchstartwe look at the changedTouches object of the Event objectwhich contains information on each touch point initiated by that touch event on the touch surface.

Here we're only interested in the first touch point ie: finger that has made contact, specifically, its pageX coordinate on the page when the touch is made. The Event object whenever a touch event is fired holds a wealth of information about the touch action; you already saw its changedTouches object, which contains information on touch points changed since the last touch event.

Lets take the above example a bit further now, by bringing in the touchmove and touchend events to show the distance traveled by a touch action from beginning to end on a DIV, from a finger touching down on an object to lifting up. Touch Me! Touch then move your finger to see the current state of the touch and the distance traveled. We call event. In the case of touchstart and touchend for instance, if the bound-to element was a link, not suppressing the default action would cause the browser to navigate to the link, cutting short our custom sequence of actions.

In the case of touchmovecalling event. Once again, we access the first element inside event. This property is adequate for what we're trying to do here, which is simply to get the relative distance traveled while a touch is maintained on the element. To get the distance traveled between touchstart and touchend events, we define a startx variable at the touchstart phase that gets the starting clientX position of the touch.

Then throughout the touchmove event, we get the clientX position of the touch and subtract from it the startx value to get the distance traveled while the touch point is maintained. Notice how the touchend event is still fired and displays the final resting x coordinates even if your finger is outside the bound-to element at the time of lifting up your finger.

The object event. It's high time to look at this object in detail now. The Event object is this mystical unicorn in JavaScript that contains information on an event when it occurs, whether it's the URL of a link in an onclick event, the keyCode of the key pressed in an onkeypress event etc.Today, most Web content is designed for keyboard and mouse input.

However, devices with touch screens especially portable devices are mainstream and Web applications can either directly process touch-based input by using Touch Events or the application can use interpreted mouse events for the application input. A disadvantage to using mouse events is that they do not support concurrent user input, whereas touch events support multiple simultaneous inputs possibly at different locations on the touch surfacethus enhancing user experiences.

The touch events interfaces support application specific single and multi-touch interactions such as a two-finger gesture. A multi-touch interaction starts when a finger or stylus first touches the contact surface.

Other fingers may subsequently touch the surface and optionally move across the touch surface. The interaction ends when the fingers are removed from the surface. During this interaction, an application receives touch events during the start, move, and end phases. The application may apply its own semantics to the touch inputs. Touch events consist of three interfaces TouchTouchEvent and TouchList and the following event types:. The Touch interface represents a single contact point on a touch-sensitive device.

The contact point is typically referred to as a touch point or just a touch. A touch is usually generated by a finger or stylus on a touchscreen, pen or trackpad.

Using Touch Events

A touch point's properties include a unique identifier, the touch point's target element as well as the X and Y coordinates of the touch point's position relative to the viewport, page, and screen. The TouchList interface represents a list of contact points with a touch surface, one touch point per contact.

Thus, if the user activated the touch surface with one finger, the list would contain one item, and if the user touched the surface with three fingers, the list length would be three. The TouchEvent interface represents an event sent when the state of contacts with a touch-sensitive surface changes. The state changes are starting contact with a touch surface, moving a touch point while maintaining contact with the surface, releasing a touch point and canceling a touch event.

This interface's attributes include the state of several modifier keys for example the shift key and the following touch lists:. Together, these interfaces define a relatively low-level set of features, yet they support many kinds of touch-based interaction, including the familiar multi-touch gestures such as multi-finger swipe, rotation, pinch and zoom. An application may consider different factors when defining the semantics of a gesture.

For instance, the distance a touch point traveled from its starting location to its location when the touch ended. Another potential factor is time; for example, the time elapsed between the touch's start and the touch's end, or the time lapse between two simultaneous taps intended to create a double-tap gesture. The directionality of a swipe for example left to right, right to left, etc. The touch list s an application uses depends on the semantics of the application's gestures.

For example, if an application supports a single touch tap on one element, it would use the targetTouches list in the touchstart event handler to process the touch point in an application-specific manner.

If an application supports two-finger swipe for any two touch points, it will use the changedTouches list in the touchmove event handler to determine if two touch points had moved and then implement the semantics of that gesture in an application-specific manner. Browsers typically dispatch emulated mouse and click events when there is only a single active touch point.

Multi-touch interactions involving two or more active touch points will usually only generate touch events. To prevent the emulated mouse events from being sent, use the preventDefault method in the touch event handlers.

For more information about the interaction between mouse and touch events, see Supporting both TouchEvent and MouseEvent.

double touch event javascript

This section contains a basic usage of using the above interfaces. See the Touch Events Overview for a more detailed example. The touch events browser compatibility data indicates touch event support among mobile browsers is relatively broad, with desktop browser support lagging although additional implementations are in progress.In order to provide quality support for touch-based user interfaces, touch events offer the ability to interpret finger or stylus activity on touch screens or trackpads.

Multi-touch interaction

The touch events interfaces are relatively low-level APIs that can be used to support application specific multi-touch interactions such as a two-finger gesture. A multi-touch interaction starts when a finger or stylus first touches the contact surface. Other fingers may subsequently touch the surface and optionally move across the touch surface.

The interaction ends when the fingers are removed from the surface. During this interaction, an application receives touch events during the start, move and end phases. Touch events are similar to mouse events except they support simultaneous touches and at different locations on the touch surface. The TouchEvent interface encapsulates all of the touch points that are currently active. The Touch interface, which represents a single touch point, includes information such as the position of the touch point relative to the browser viewport.

This example tracks multiple touch points at a time, allowing the user to draw in a element with either the canvas scripting API or the WebGL API to draw graphics and animations. It will only work on a browser that supports touch events. When a touchstart event occurs, indicating that a new touch on the surface has occurred, the handleStart function below is called. This calls event. Then we get the context and pull the list of changed touch points out of the event's TouchEvent.

After that, we iterate over all the Touch objects in the list, pushing them onto an array of active touch points and drawing the start point for the draw as a small circle; we're using a 4-pixel wide line, so a 4 pixel radius circle will show up neatly. Each time one or more fingers moves, a touchmove event is delivered, resulting in our handleMove function being called.

Its responsibility in this example is to update the cached touch information and to draw a line from the previous position to the current position of each touch. This iterates over the changed touches as well, but it looks in our cached touch information array for the previous information about each touch in order to determine the starting point for each touch's new line segment to be drawn.

This is done by looking at each touch's Touch. This property is a unique integer for each touch, and remains consistent for each event during the duration of each finger's contact with the surface. This lets us get the coordinates of the previous position of each touch and use the appropriate context methods to draw a line segment joining the two positions together. After drawing the line, we call Array. When the user lifts a finger off the surface, a touchend event is sent.

We handle this by calling the handleEnd function below. Its job is to draw the last line segment for each touch that ended and remove the touch point from the ongoing touch list. This is very similar to the previous function; the only real differences are that we draw a small square to mark the end and that when we call Array. The result is that we stop tracking that touch point. If the user's finger wanders into browser UI, or the touch otherwise needs to be canceled, the touchcancel event is sent, and we call the handleCancel function below.Jakob Jenkov Last update: Handling touch events in JavaScript is done by adding touch event listeners to the HTML elements to handle touch events for.

This is done similarly to adding a click listener:. Touch events are somewhat more complex than mouse events. Like with a mouse you can listen for touch down, touch move, touch end etc. Not all browsers may fire all of these events I have had problems getting Chrome to fire touch leave events.

A tap is like a mouse click. The user taps a button or link just like they would click it with a mouse. Mobile browsers will typically convert a tap on a button, link etc. The browser waits ms to see if the user performs any more advanced touch gestures, before firing the click event.

This is done to make sure that it is actually a click event that should get fired.

double touch event javascript

These ms makes your web app feel laggy compared to native apps. Web apps already have a disadvantage compared to native apps, and those ms just makes that worse. Therefore we want to listen for touch events in touch enabled browsers. It is not enough to just listen for touch events though.

You want your app to be usable in desktop browsers too which are not touch enabledso you have to listen for both click and touch events. Here is how that is done:. When you listen for both touch and click events, you have a slight problem in touch enabled browsers. Even if a touch event is fired immediately after a tap, an additional click event is still fired after ms.

That means that your listener function will get called twice! To avoid the double calls to the listener function, you call the event. This disables the browsers default behaviour, and lets your code handle the touch event. This disables the firing of the click event ms after the touch event.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here.

Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. My site is in magento open source ecommerce solution, and in the top header it has a shopping cart icon. On dekstop, when user hovers mouse over that icon, it shows the content items in shopping cart.

But on click of that icon, it takes the user to shopping cart page. I want to have the ability, only for touch devices, that when a user single tap on icon, it should display the content only, but when double tap the icon, it should take them to the cart page. This code does prevent from default behaviour on single tap but does not show content div, also does not do anything on double tap. If you want to target mobile then you can use touchstart which is the touch equivalent of mousedown. Last I checked, there wasn't any built in "double tap" event we can hook onto, but we can manually check for a tap or a double tap.

The code would look something like. An alternative, though a bit heavy for just this, is to use Hammer. It's a really nice library for handling touch events. And it also aggregates mouse and touch events into one type of event. For example, for tap and double tap it'll essentially be.

This also works for mouse events too, which might not be what you want. And it is a bit overkill if it's just for this. But if you plan to do more touch related stuff, I would recommend using this library.

This is the final working code that did the magic for me : I have just copied and paste all the code in here but one may use it in accordance to own needs.

It's a small code block and it should work well on mobiles.

double touch event javascript

It sets a timer when you first tap on something and then it checks if the second tap is in the time range ms in this example. Learn more. Ask Question. Asked 5 years, 10 months ago. Active 7 months ago.


Thoughts to “Double touch event javascript”

Leave a Comment