- $.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);
- }
- }
首先要添加这段代码, 然后调用:
- $('.object').longPress(function(){
- //do something...
- });
来源: http://www.bubuko.com/infodetail-3119505.html