JS识别长按案例源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <!DOCTYPE html> < html > < head > < title >JS识别长按案例源码</ title > < meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" > < meta name = "viewport" content = "width=device-width,initial-scale=1,maximum-scale=1" /> < meta name = "apple-touch-fullscreen" content = "yes" > < meta name = "apple-mobile-web-app-capable" content = "yes" /> < link href = "css/travelstyle.css" rel = "stylesheet" type = "text/css" /> </ head > < body > < div style = "width:100%;" > < h2 >JS识别长按案例源码 - 作用:判断用户有没有长按识别二维码等</ h2 > < img src = "./youhutong.png" ontouchstart = "gtouchstart()" ontouchmove = "gtouchmove()" ontouchend = "gtouchend()" /> </ div > < script > //定时器 var timeOutEvent=0; //开始按 function gtouchstart(){ //这里设置定时器,定义长按500毫秒触发长按事件,时间可以自己改 timeOutEvent = setTimeout("longPress()", 500); return false; }; //手释放,如果在500毫秒内就释放,则取消长按事件,此时可以执行onclick应该执行的事件 function gtouchend(){ //清除定时器 clearTimeout(timeOutEvent); if(timeOutEvent!=0){ //这里写要执行的内容(尤如onclick事件) alert("你这是点击,不是长按"); } return false; }; //如果手指有移动,则取消所有事件,此时说明用户只是要移动而不是长按 function gtouchmove(){ //清除定时器 clearTimeout(timeOutEvent); timeOutEvent = 0; }; //真正长按后应该执行的内容 function longPress(){ timeOutEvent = 0; //执行长按要执行的内容,如弹出菜单 alert("长按事件触发发"); } </ script > </ body > </ html > |
转载请注明来源地址:小川编程 » https://www.youhutong.com/index.php/article/index/232.html
1、本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,下载后请24小时内删除。
2、本站所有内容均不能保证其完整性,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用
3、本站联系方式Email:admin@youhutong.com ,收到邮件会第一时间处理。
4、如侵犯到任何版权问题,请立即告知本站(立即在线告知),本站将及时删除并致以最深的歉意