对于复活节假期,我想在我开发的网站上有一个惊喜“复活节彩蛋”。我隐藏的这五个复活节彩蛋中的两个将是按键命令。这不会像“按CTRL和TAB同时”类型的交易,但将是“Pres up三次然后RIGHT三次”类型的事情。这将是寻找一系列按键而不是一次按下两个按键。我已经设置了这个功能,但由于一些奇怪的原因它不能正常工作。
注意:以下脚本正在寻找以下按键印章:
surprise1 - LEFT(x3),RIGHT(x3),UP(x3),DOWN(x3)
surprise2 - SHIFT(x3),TAB(x3),CTRL(x3)
$(document.body).keydown(function(e) {
surprise1(e);
surprise2(e);
});
function surprise1(e) {
var ev = (e) ? e : window.event;
var k = ev.keyCode;
if (k > 36 && k < 41) {
typekeys[k] = isNaN(typekeys[k]) ? 0 : typekeys[k];
typekeys[k]++;
if (typekeys[37] == 3) {
if (typekeys[37] == 3 && typekeys[39] == 3) {
if (typekeys[37] == 3 && typekeys[39] == 3 && typekeys[38] == 3) {
if (typekeys[37] == 3 && typekeys[39] == 3 && typekeys[38] == 3 && typekeys[40] == 3) {
alert("You've found Surprise 1! Contact the site admin ASAP to get your prize!");
typekeys[37] = typekeys[39] = typekeys[38] = typekeys[40] = 0;
}
} else {
typekeys[40] = 0;
}
} else {
typekeys[38] = typekeys[40] = 0;
}
} else {
if (typekeys[37] > 3) {
typekeys[37] = 0;
}
typekeys[39] = typekeys[38] = typekeys[40] = 0;
}
} else {
typekeys[37] = typekeys[39] = typekeys[38] = typekeys[40] = 0;
}
};
function surprise2(e) {
var ev = (e) ? e : window.event;
var k = ev.keyCode;
if (k > 8 && k < 18) {
typekeys[k] = isNaN(typekeys[k]) ? 0 : typekeys[k];
typekeys[k]++;
if (typekeys[16] == 3) {
if (typekeys[9] == 3) {
if (typekeys[16] == 3 && typekeys[9] == 3 && typekeys[17] == 3) {
alert("You've found Surprise 2! Contact the site admin ASAP to get your prize!");
typekeys[16] = typekeys[9] = typekeys[17] = 0;
}
}
} else {
if (typekeys[16] > 3) {
typekeys[16] = 0;
}
typekeys[9] = typekeys[17] = 0;
}
} else {
typekeys[16] = typekeys[9] = typekeys[17] = 0;
}
};
请注意告诉我为什么不起作用?在我看来它应该有用。
答案 0 :(得分:2)
试试这个:我正在使用https://github.com/madrobby/keymaster jquery插件
$(function () {
var combination = ''
key('left', function(){
combination = 'left';
checkCombination();
});
key('right', function(){
combination+= 'right';
checkCombination();
});
key('up', function(){
combination+= 'up';
checkCombination();
});
key('down', function(){
combination+= 'down';
checkCombination();
});
function checkCombination() {
if(combination === 'leftrightupdown') {
alert('surprise 1');
}
}
});
答案 1 :(得分:1)
这是我的解决方案。我必须做一些有趣的事情来比较数组here。我相信你可以适应这个剧本的一般要点,以满足你的需求....
var seqs = [ [37,37,37,38,38,38,39,39,39,40,40,40], [9,9,9,16,16,16,17,17,17] ];
var seq = [];
var messages=["You've found Surprise 1! Contact the site admin ASAP to get your prize!", "You've found Surprise 2! Contact the site admin ASAP to get your prize!"];
window.addEventListener("keydown", function(e){
seq.push(e.keyCode);
var eq = function(a,b){ return !( a<b || b<a ); };
for ( var i = 0; i < seqs.length; i++ ) {
if ( eq( seq, seqs[i].slice(0,seq.length) )) {
if ( eq(seq, seqs[i]) ) {
alert( messages[i] );
seq = [];
}
return;
}
}
seq = [];
});
答案 2 :(得分:1)
以下是我解决这个问题的方法......
var nums = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
var n = nums.slice();
$(document).keydown(function(e){
if(e.which == n[0]){
n.shift();
} else n = nums.slice();
if(n.length == 0) {
//success!
n = nums.slice();
}
});
如果您知道序列是什么,那就值得称道。 ;)
答案 3 :(得分:0)
这样更好:
$(function () {
var combination = ''
key('left', function(){
combination = 'left';
checkCombination();
});
key('right', function(){
combination+= 'right';
checkCombination();
});
key('up', function(){
combination+= 'up';
checkCombination();
});
key('down', function(){
combination+= 'down';
checkCombination();
});
key(!'down' && !'left' && !'right' && !'up',function() {
combination = '';
});
function checkCombination() {
if(combination === 'leftrightupdown') {
alert('surprise 1');
}
}
});