Home >Web Front-end >JS Tutorial >JavaScript Konami Code implementation code_javascript skills

JavaScript Konami Code implementation code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:49:081040browse

There is an easter egg on Facebook:
Log in to facebook.com, click anywhere on your homepage, enter Up, Up, Down, Down, Left, Right, Left, Right, B, A, Enter on the keyboard, and then click on the page Or scroll the scroll bar, you will find special changes (as shown below), hehe ^^

Facebook 上有一个彩蛋

Friends who have played "Contra" will definitely be able to tell at a glance that the characters entered are actually the "secret skills" in "Contra". In fact, the term "secret technique" is called Konami Code, see the picture for details:

Konami Code

So how can you use JavaScript to add a similar easter egg to your own page?
Abhi provides a general idea in the article "Konami Code on Facebook: How to implement it on your site":

Copy code The code is as follows:

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

However, Abhi’s method is still a bit redundant, Jan Jarfalk left a message A short and concise code is provided:
Copy the code The code is as follows:

var k=[] ;
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");
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn