有时在作移动端页面开发过程当中遇到这种需求:''模拟指纹识别''。
实际上咱们只能经过长按页面中的元素来模拟这个功能。
在jQuery和Zepto中都没有包含长按事件,因此须要咱们来扩展一下。this
$.fn.longPress = function(fn) { var timeout = undefined; var $this = this; for(var i = 0;i<$this.length;i++){ $this[i].addEventListener('touchstart', function(event) { timeout = setTimeout(fn, 800); //长按时间超过800ms,则执行传入的方法 }, false); $this[i].addEventListener('touchend', function(event) { clearTimeout(timeout); //长按时间少于800ms,不会执行传入的方法 }, false); } }
首先要添加这段代码,而后调用:code
$('.object').longPress(function(){ //do something... });