1. 程式人生 > >移動端長按事件——jquey例子

移動端長按事件——jquey例子


$("#target").on({  
        touchstart: function(e) { 
            // 長按事件觸發  
            timeOutEvent = setTimeout(function() {  
                timeOutEvent = 0;  
                alert('你長按了');  
            }, 400);  
            //長按400毫秒   
            // e.preventDefault();    
        },  
        touchmove: function() {  
            clearTimeout(timeOutEvent);  
            timeOutEvent = 0;  
        },  
        touchend: function() {  
            clearTimeout(timeOutEvent);  
            if (timeOutEvent != 0) {  
                // 點選事件  
                // location.href = '/a/live-rooms.html';  
                alert('你點選了');  
            }  
            return false;  
        }  
    })

1.使用移動端事件,touchstart、touchmove、touchend來實現長按事件。

2.touchstart事件下,執行定時器,處理長按要做的操作。touchmove、touchend時清除定時器