Jquery
$.fn.longpress =function(callback) {varTimer =undefined; var$ This= This; //support for binding multiple elements for(vari = 0; I < $ This. length; i++) { $ This[I].addeventlistener (' Touchstart ',function(event) {Timer= SetTimeout (Callback, 500); }, false); $ This[I].addeventlistener (' Touchmove ',function(event) {cleartimeout (timer); }, false); $ This[I].addeventlistener (' Touchend ',function(event) {cleartimeout (timer); }, false); } }
Native implementation
<!DOCTYPE HTML><HTML><Head> <MetaCharSet= "Utf-8"/> <title></title> <Scripttype= "Text/javascript"src= "Http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></Script></Head><Body><Divstyle= "width:100%;"> <DivID= "Toucharea"style= "width:90%; height:200px; Background-color: #CCC; font-size:100px">Long press My</Div> </Div><Script>vartimeoutevent=0;$(function(){ $("#touchArea"). On ({touchstart:function(e) {timeoutevent=SetTimeout ("longpress ()", -); E.preventdefault (); }, Touchmove:function() {cleartimeout (timeoutevent); Timeoutevent= 0; }, Touchend:function() {cleartimeout (timeoutevent); if(Timeoutevent!=0) {alert ("you This is click, not long press"); } return false; } })}); functionlongpress () {timeoutevent= 0; Alert ("Long Press events trigger the hair"); } </Script></Body></HTML>
JS Gesture Long Press Code