2015-10-08 34 views
2
var gameLink = $("#theGame"); 
$(window).keydown(function (e) { 
    if (e.which === 32) { 
     window.location.href = "thegame.html; 
    } 
}); 

我很新的jQuery和我似乎無法找出其中用於移動添加「水龍頭」事件給這個代碼。「點擊」移動增加「的keydown」事件的jQuery

+0

缺少'「''的location.href' – Tushar

+1

你可以嘗試'$(窗口)。在(」 KEYDOWN水龍頭」,函數(){' –

+0

@GuruprasadRao感謝您的幫助:) – Mar

回答

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'); 
    } 

}); 
+0

非常感謝! – Mar

+1

@Mar ..我建議,以紀念這個崗位作爲答案,如果它幫助你.. –