Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Web App touch gestures

I've searched all across the web to find a simple way of adding touch gestures to a simple button. Basically I'm trying to find a simple way of getting the back button (which you usually see on the task-bar at the top of an iOS device) to change CSS classes from 'normal' state to 'pressed' state when pressed.

Although I'm very new to Javascript, I would prefer to use standard DOM methods rather than jQuery (or any other library). Would anyone have some complete code and explain how the JavaScript code reads an ontouchstart and ontouchend event and how these functions could be used to change CSS classes?

Any help would be greatly appreciated!

TC

like image 838
TronCraze Avatar asked Jun 04 '26 22:06

TronCraze


2 Answers

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.

like image 186
lomanf Avatar answered Jun 07 '26 11:06

lomanf


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', ''); };
like image 23
Luke Dennis Avatar answered Jun 07 '26 12:06

Luke Dennis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!