修身养性,知行合一

  • 首页
  • 爱码
    • 系统
    • 数据库
    • JavaScript
    • CSharp
    • Python
  • 生活
    • 文化
    • 美食
  • 杂谈
  • 关于
修身养性,知行合一
码字,杂谈
  1. 首页
  2. 爱码
  3. 前端
  4. JavaScript
  5. 正文

写个小彩蛋

2025年7月9日 9点热度 0人点赞 0条评论

玩儿过 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('🎉 恭喜你发现了隐藏彩蛋!你是真正的开发者!');
}

在当前页面就可以看到效果。

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: JavaScript
最后更新:2025年7月9日

jeremyjone

这个人很懒,什么都没留下

打赏 点赞
< 上一篇

文章评论

取消回复

最新 热点 随机
最新 热点 随机
写个小彩蛋 绘制一个可重用的线条阴影 node-sass 的安装 解决端口被占的问题 vue3 组件 Props 的声明方式 给 div 添加选中状态
绘制一个可重用的线条阴影写个小彩蛋
Windows Server 配置域内机器自动登录 Windows下管理Linux格式硬盘 群辉 RAID1 数据恢复小记 手动实现JS防抖 vue2 使用 @vue/composition-api 的一些问题 windows下mysql自动备份

(っ•̀ω•́)っ✎⁾⁾ 开心每一天

COPYRIGHT © 2021 jeremyjone.com. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

京ICP备19012859号-1

京公网安备 11010802028585号