我正在將一個Konami復活節彩蛋放入我的網站以供踢腿;你輸入Konami密碼,它會播放街頭霸王的誇張聲音,背景中的歌舞伎面具上的眼睛會亮起一秒鐘。我該如何獲得這個Javascript函數來正確迭代?
它有效...有點。我遇到了問題。輸入Konami代碼後,每次用戶按下一個鍵時,上述效果都會迭代。這並不意味着要這樣做。每次用戶輸入完整的代碼時,我只想立即關閉效果。
還有另一個更小的呃逆,會很好解決。通過簡單地將初始背景切換爲具有期望效果的一個,實際上使面具的眼睛發亮。但是,第一次完成時,頁面會在第二個背景加載時閃爍,但後續迭代中不會有閃光。我的想法是將第二個背景加載爲HTML圖像,並將其可見性設置爲隱藏,但這並不起作用。
<script>
function PlaySound(path)
{
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', path);
audioElement.play();
}
function resetBackground()
{
document.body.style.backgroundImage = "url('onics.png')";
}
function flashBackground()
{
document.body.style.backgroundImage = "url('onics_red.png')";
setTimeout(resetBackground, 830);
}
if(window.addEventListener)
{
var kkeys=[],konami="38,38,40,40,37,39,37,39,66,65";
window.addEventListener("keydown",function(e)
{
kkeys.push(e.keyCode);
if(kkeys.toString().indexOf(konami)>=0)
{
PlaySound('hadouken.wav');
flashBackground();
}
},true);
}
</script>
這樣做。謝謝,jcubic。 –