Konami Code 出现在很多游戏当中,也出现在像Facebook和Greader的彩蛋当中,键盘顺序是:上 上 下 下 左 右 左 右 B A
法一:
var $ = {
enabled: false,
tmp: Array(),
_konamiCode: Array(65,66,39,37,39,37,40,40,38,38),
init: function() {
this.tmp = Array(65,66,39,37,39,37,40,40,38,38);
},
konamiCode: function(e) {
if(!this.enabled) {
var t = this.tmp.pop();
if((e.keyCode-t) == 0) {
if(this.tmp.length == 0) {
this.enabled = true;
}
} else {
this.init();
}
} else {
this.action();
}
},
// Change the action() function to whatever you want to
action: function() {
//alert(”Konami Code Activated”);
}
}
$.init();
document.onkeydown = $.konamiCode;
法二:
// Tweetable Konami code
var k=[];
document.onkeydown = function(e){
k.push(e.keyCode);
if(k.toString().indexOf(”38,38,40,40,37,39,37,39,66,65″)>=0) {
//alert(”Konami Code Activated”);
}
}
法三:
function onKonamiCode(fn) {
var codes = (function(){
var c = [38,38,40,40,37,39,37,39,66,65];
onKonamiCode.requireEnterKey && c.push(13);
return c;
})(),
expecting = function(){
expecting.codes = expecting.codes || Array.apply({}, codes);
expecting.reset = function() { expecting.codes = null; };
return expecting.codes;
},
handler = function(e) {
if (expecting()[0] == (e||window.event).keyCode) {
expecting().shift();
if (!expecting().length) {
expecting.reset();
fn();
}
} else { expecting.reset(); }
};
window.addEventListener ?
window.addEventListener(’keydown’, handler, false)
: document.attachEvent(’onkeydown’, handler);
}
onKonamiCode(function(){alert(”Konami Code Activated”);});
个人觉得第三种方法比较赞~