“点击”移动添加到“keydown”事件jquery

时间:2015-10-08 04:09:30

标签: javascript jquery mobile

var gameLink = $("#theGame");
$(window).keydown(function (e) {
    if (e.which === 32) {
        window.location.href = "thegame.html;
    }
});

我是jQuery的新手,我似乎无法弄清楚将移动设备的“tap”事件添加到此代码的位置。

1 个答案:

答案 0 :(得分:1)

这几乎是你所需要的:http://jsfiddle.net/gianlucaguarini/56Szw/

来自小提琴的代码

var getPointerEvent = function(event) {
    return event.originalEvent.targetTouches ? event.originalEvent.targetTouches[0] : event;
};
var $touchArea = $('#touchArea'),
    touchStarted = false, // detect if a touch event is sarted
    currX = 0,
    currY = 0,
    cachedX = 0,
    cachedY = 0;

//setting the events listeners
$touchArea.on('touchstart mousedown',function (e){
    e.preventDefault(); 
    var pointer = getPointerEvent(e);
    // caching the current x
    cachedX = currX = pointer.pageX;
    // caching the current y
    cachedY = currY = pointer.pageY;
    // a touch event is detected      
    touchStarted = true;
    $touchArea.text('Touchstarted');
    // detecting if after 200ms the finger is still in the same position
    setTimeout(function (){
        if ((cachedX === currX) && !touchStarted && (cachedY === currY)) {
            // Here you get the Tap event
            $touchArea.text('Tap');
        }
    },200);
});
$touchArea.on('touchend mouseup touchcancel',function (e){
    e.preventDefault();
    // here we can consider finished the touch event
    touchStarted = false;
    $touchArea.text('Touchended');
});
$touchArea.on('touchmove mousemove',function (e){
    e.preventDefault();
    var pointer = getPointerEvent(e);
    currX = pointer.pageX;
    currY = pointer.pageY;
    if(touchStarted) {
         // here you are swiping
         $touchArea.text('Swiping');
    }

});