简单的写一了一个基于jquery:
(function($) {
$.extend($.fn, {
longPress : function(time,callBack){
time = time || 1000;
var timer = null;
$(this).mousedown(function(e){
var i = 0;
var _this = $(this);
timer = setInterval(function(){
i+=10;
if(i >= time) {
clearTimeout(timer);
var positionX = e.pageX - _this.offset().left || 0;
var positionY = e.pageY - _this.offset().top || 0;
typeof callBack == 'function' && callBack.call(this,positionX,positionY);
}
},10)
}).mouseup(function(){clearTimeout(timer);})
}
});
}) (jQuery);
用法:
obj.longPress(500,function(){
//长按 500 毫秒触发
});
将鼠标在当前元素的x,y轴坐标返回:
obj.longPress(800,function(x,y){
//长按 800 毫秒触发
alert("x:"+x+',y:'+y);
});
完整例子:
$('.test').longPress(500,function(x,y){
alert("x:"+x+',y:'+y);
//触发事件
})