Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating touchstart and touchend events?

I'm developing a jquery component which works primarily for ipad. So is there anyway to simulate 'touchstart and 'touchend' events in desktop rather than having the device itself to check the events.

like image 643
user1184100 Avatar asked May 02 '12 02:05

user1184100


People also ask

What is the touchstart event?

The touchstart event occurs when the user touches an element. Note: The touchstart event will only work on devices with a touch screen. The numbers in the table specify the first browser version that fully supports the event.

What is the touchend event?

The touchend event occurs when the user removes the finger from an element. Note: The touchend event will only work on devices with a touch screen. The numbers in the table specify the first browser version that fully supports the event.

How to prevent the event handler from responding to touchstart and clicks?

Use of the preventDefault () or stopPropagation () method: This method prevents the event handler from responding to both touchstart and clicks events.

How to execute a JavaScript event when the user touches an element?

Execute a JavaScript when the user touches a P element (for touch screens only): The touchstart event occurs when the user touches an element. Note: The touchstart event will only work on devices with a touch screen. The numbers in the table specify the first browser version that fully supports the event.


3 Answers

You can author your own custom events within jQuery:

var event = $.Event( "touchstart", { pageX:200, pageY:200 } );

And you can issue them against any element in the DOM:

$("body").trigger( event );

Demo: http://jsbin.com/ezoxed/edit#javascript,html
Further reading: http://api.jquery.com/category/events/event-object/

Keep in mind that there are various other types of interfaces on the market now that don't support touchstart and touchend events. For instance, Windows 8 is already occupying tablets in the mobile market, and it uses a more abstracted event model consisting of Pointers.

like image 51
Sampson Avatar answered Oct 17 '22 02:10

Sampson


Chrome Dev-tools within a Chrome Browser allows you to emulate touch events. See https://developers.google.com/chrome-developer-tools/docs/mobile-emulation.

From the docs...

Emulating Touch Events

Touch is an input method that's difficult to test on the desktop, since most desktops don't have touch input. Having to test on mobile can lengthen your development cycle, since every change you make needs to be pushed out to a server and then loaded on the device.

A solution to this problem is to simulate touch events on your development machine. For single-touches, the Chrome DevTools supports single touch event emulation to make it easier to debug mobile applications on the desktop.

To use from a Chrome browser (as of version 29.0.1547.65):

  1. Select the Chrome menu at the top-right of your browser window (three stacked lines).
  2. Select Tools > Developer tools. (Shortcut Shift+Control+I)
    A tools window will appear on the bottom with the tab Console selected.
  3. In the bottom right click on the settings cog (look like a gear).
    A setting panel will appear with "General" on top.
  4. Click "Overrides" on left to select overrides panel.
  5. Scroll down and check "Enable touch events"
  6. Reload your page

You mouse will now appear as a fuzzy circle. Click to "touch".

like image 6
Thaddeus Albers Avatar answered Oct 17 '22 01:10

Thaddeus Albers


As of 2018, Chrome DevTools supports device emulation outright, without any need for override setting. Just toggle the device toolbar (Ctrl + Shift + M) to get the browser into mobile mode, then touch events can be triggered by the mouse.

like image 2
prusswan Avatar answered Oct 17 '22 00:10

prusswan