Skip to main content

Javascript and Touch devices.

If you are working on draggin and dropping some stuffs on html, you must be aware of whether it still works on mobile devices which support touch function. Maybe you could consider using JQuery UI plugin which allows "droppable" elements. jQuery Plugin Or here is another descent plugin iPhone Touch

Touch Event for webkit

touchstart - triggered when a touch is initiated. Mouse equivalent - mouseDown
touchmove - triggered when a touch moves. Mouse equivalent - mouseMove
touchend - triggered when a touch ends. Mouse equivalent - mouseUp.
This one is a bit special on the iPhone - see below
touchcancel - bit of a mystery

Example

docuement.addEventListener('touchstart', function(event) {

event.preventDefault();

var touch = event.touches[0];

alert("Touch x:" + touch.pageX + ", y:" + touch.pageY);

}, false);

The event object:

  • touches - contains touch information upon touchStart and touchMove not touchEnd events
  • targetTouches - contains information for touches originating from the same target element
  • changedTouches - contains touch information upon all touch events

Example

document.addEventListener('gesturechange', function(event) {

event.preventDefault();

alert("Scale: " + event.scale + ", Rotation: " + event.rotation);

}, false);

Sometimes you can have trouble when you try to get touch coordinates relative to the
viewpoint of browser from touch event. ClientX/Y properties have offset values including scrolling.
One way of solution you can have is below

function fixTouch (touch) {

var winPageX = window.pageXOffset,

winPageY = window.pageYOffset,

x = touch.clientX,

y = touch.clientY;

if (touch.pageY === 0 && Math.floor(y) > Math.floor(touch.pageY) ||

touch.pageX === 0 && Math.floor(x) > Math.floor(touch.pageX)) {

// iOS4 clientX/clientY have the value that should have been

// in pageX/pageY. While pageX/page/ have the value 0

x = x - winPageX;

y = y - winPageY;

} else if (y < (touch.pageY - winPageY) || x < (touch.pageX - winPageX) ) {

// Some Android browsers have totally bogus values for clientX/Y

// when scrolling/zooming a page. Detectable since clientX/clientY

// should never be smaller than pageX/pageY minus page scroll

x = touch.pageX - winPageX;

y = touch.pageY - winPageY;

}

return { clientX: x, clientY: y };

}

Events Table

tuchstart touchmove touchend gesturestart gesturemove gestureend
Android y y y n n n
iPhone y y y y y y
has event.touches y y y n n n
(iPhone) has event.scale and event.rotation n n n y y y
(iPhone) touch in event.touches y y n - - -
(iPhone) touch in event.changedTouches y y y - - -


Comments

Popular posts from this blog

ASP.NET Core & Angular 2 - Chapter 2 Routing

Route to a component The Angular 2 router is a powerful tool which enables you to do just that. Specifically you can configure it to route to a component when the user enters a specific url for your site. So in this case, we could configure it so that navigating to http://domain/hello routes directly to the hello world component. Open up your app.module.ts file and make the following simple change in routing section. 1 2 3 4 5 6 7 8 9 10 11 imports: [      DefaultModules, //This automatically imports BrowserModule, HttpModule, and JsonpModule too.      RouterModule.forRoot([          { path: '' , redirectTo: 'home' , pathMatch: 'full' },          { path: 'home' , component: HomeComponent },          { path: 'counter' , component: CounterComponent },       ...

ASP.NET 4.0 with Ajax

It's common for an web application to retrieve data from server and bind to control. Control being alone without data binding is nothing more than styled modeling statue. So interacting between DB and web control comes to challenge for developers whenever we design its working procedure. Ajax is possible only with a strong JavaScript engine that runs within the client browser and provides the foundation for more advanced and asynchronous features. The Javascript library currently incorporated in ASP.NET 3.5 Service Pack 1 is a necessary, but insufficient, attempt to deliver such a library. A more powerfule ASP.NET AJAX platform is required and it is just being introduced as part of ASP.NET AJAX 4.0 Requirements for Client-Side Data Binding There are two fundamental patterns for implementing data binding functionalities. One is the HTML Message pattern, and the other is the Browser-side Template pattern. The former entails making a remote service call to a component that re...