iOS Web App touch gestures

折月煮酒 提交于 2019-12-03 00:47:07

ontouchstart, ontouchmove and ontouchend are managed the same as onclick, onmousemove and so.

You can apply the listeners in a <script> tag or directly in the html element.

Using JavaScript only


var back = document.getElementById("back-button-id");

back.ontouchstart = function( event ) {
 // using the target property of the event
 // you can reach the hitted html element
 event.target.className = 'css-href-selected-class-name';
}

back.ontouchend = function( event ) {
 event.target.className = 'css-href-normal-class-name';
}

Using HTML tag and callbacks

1) Declare your Javascript callbacks to swap a css class for any state


function onclickCallback( event ) {
 // do something
}

function ontouchstartCallback( event ) {
 event.target.className = 'selected';
}

function ontouchendCallback( event ) {
 event.target.className = 'normal';
}

2) Put the callbacks into the anchor tag (I suggest to use DIV instead of A)


<div class="normal" onclick="onclickCallback( event );" ontouchstart="ontouchstartCallback( event );" ontouchend="ontouchendCallback( event );">Back</div>

Edit 1: to prevent hilight freezing during scrolling

Try to add the ontouchmove handler

ontouchmove="ontouchmoveCallback( event );"

Then declare the handler function that swap the css class

function ontouchmoveCallback( event ) {
    event.target.className = 'normal';
}

Hope this helps! Ciao.

This should get you started:

HTML:

 <input type="button" id="thebutton" value="Do Stuff!" />

Javascript:

 var thebutton = document.getElementById("thebutton");

 thebutton.ontouchstart = function(e)
 {
     this.setAttribute('class', 'pressed');

     var touches = e.touches; // array of all touch data

     var target = touches[0].target; // what DOM element was touched
     var pageX = touches[0].pageX; // coords relative to site
     var pageY = touches[0].pageY;
     var clientX = touches[0].clientX; // coords relative to screen
     var clientY = touches[0].clientY;
 };

 thebutton.ontouchmove = function(e)
 {
     var touches = e.touches; // same fields as above
     var changedTouches = e.changedTouches; // only touches which have changed
 };

 thebutton.ontouchend = function(e)
 {
     this.setAttribute('class', '');

     // cleanup, if needed
 };

For more details, see: http://sitepen.com/blog/2008/07/10/touching-and-gesturing-on-the-iphone/

It's worth noting that MobileSafari sometimes does wonky things with touch events and form elements (input boxes in particular). You may find it's better to use a styled div than an actual input button.

EDIT: For what you're trying to do, I think you might be better served with simple click events, which generally work fine for things like button presses. Touch events are more for drag and drop, precise finger tracking etc. Try this:

thebutton.onclick = function(e) { this.setAttribute('class', 'your_class'); };

EDIT2: Now I see what you're asking for. Easiest way is this:

thebutton.ontouchstart = function(e) { this.setAttribute('class', 'pressed'); };
thebutton.ontouchend   = function(e) { this.setAttribute('class', ''); };

There are a couple of libraries already for jQuery

http://plugins.jquery.com/project/multiswipe

And you also can check this demo from

http://taitems.github.com/Mobile-Web-based-Gesture-Recognition/

And you can fork the example and start working with it.

There are some options but everything its quite new.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!