1. 程式人生 > >jquery實現手機端長按響應事件

jquery實現手機端長按響應事件

	var time = 0;//初始化起始時間


	$("#imgDiv").on('touchstart', '.previewImg', function(e){
	    e.stopPropagation();
	    var index=$("#imgDiv").find(".image").index($(this));
	    time = setTimeout(function(){
	    	showCloseImg(index);
	    }, 500);//這裡設定長按響應時間
	});
	
	$("#imgDiv").on('touchend', '.previewImg', function(e){
	    e.stopPropagation();
	    clearTimeout(time);  
	});
	
	function showCloseImg(index){
		var e=$("#imgDiv").find(".image").eq(index);
		$(".deleteImg").hide();
		e.next().show();
	}
	
如果需要去掉手機瀏覽器自帶的長按響應事件,則需要在css檔案中增加如下程式碼:
.touch_action{
-webkit-touch-callout:none;
-webkit-user-select:none;
-khtml-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;
}