玩儿过 Konami 游戏的朋友应该都知道 ↑↑↓↓←→←→BA
,我们来搞一个小彩蛋在页面中,当在页面输入这段代码后,会有一个隐藏小功能。
没啥关键点,就是纯好玩
let konamiCode = [];
const konamiSequence = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
document.addEventListener('keydown', (e) => {
konamiCode.push(e.keyCode);
if (konamiCode.length > konamiSequence.length) {
konamiCode.shift();
}
if (JSON.stringify(konamiCode) === JSON.stringify(konamiSequence)) {
triggerEasterEgg();
konamiCode = [];
}
});
做好记录,在触发方法中,我们可以做任何我们喜欢的内容。比如:
function triggerEasterEgg() {
// Create rainbow particles
for (let i = 0; i < 50; i++) {
const particle = document.createElement('div');
particle.style.cssText = `
position: fixed;
width: 10px;
height: 10px;
background: hsl(${Math.random() * 360}, 100%, 50%);
border-radius: 50%;
pointer-events: none;
z-index: 10000;
left: ${Math.random() * window.innerWidth}px;
top: ${Math.random() * window.innerHeight}px;
animation: easterEggFloat 3s ease-out forwards;
`;
document.body.appendChild(particle);
setTimeout(() => particle.remove(), 3000);
}
// Add CSS animation for easter egg
if (!document.querySelector('#easter-egg-style')) {
const style = document.createElement('style');
style.id = 'easter-egg-style';
style.textContent = `
@keyframes easterEggFloat {
0% {
transform: translateY(0) rotate(0deg);
opacity: 1;
}
100% {
transform: translateY(-200px) rotate(360deg);
opacity: 0;
}
}`;
document.head.appendChild(style);
}
console.log('🎉 恭喜你发现了隐藏彩蛋!你是真正的开发者!');
}
在当前页面就可以看到效果。
文章评论