HTML&CSS:人民币雨特效,震撼来袭!来财来财来财......

354 阅读2分钟

效果演示

这段 HTML 和 CSS 代码创建了一个简单的网页,其主要功能是模拟人民币图片像雨一样从页面顶部落下的效果。

2.gif

一键复制

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>前端Hardy</title>
    <style>
        @import url('https://fonts.googleapis.com/css?family=Nanum+Gothic');

        * {
            box-sizing: border-box;
        }

        body {
            background: #f7d6ff;
            background-image: linear-gradient(to bottom right, #91defe, #99c0f9, #bdb6ec, #d7b3e3, #efb3d5, #f9bccc);
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
            overflow: hidden;
            min-height: 100vh;
            font-family: 'Nanum Gothic', sans-serif;
            margin: 0;
        }

        h1 {
            color: #fff;
            text-shadow: 1px 1px 3px #000;
            font-size: 32px;
            position: relative;
            z-index: 2;
        }
        .yuan-container {
            position: absolute;
            animation: fall 2s linear infinite;
            text-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
        }

        @keyframes fall {
            0% {
                transform: translateY(-100vh);
                opacity: 0;
            }

            10% {
                opacity: 1;
            }

            90% {
                opacity: 1;
            }

            100% {
                transform: translateY(110vh) scale(0.5);
                opacity: 0;
            }
        }

        .yuan-icon {
            width: 200px;
            height: 100px;
            background-image: url('https://kimi-img.moonshot.cn/webimg2/k.sinaimg.cn/4cdab360c1bf98c4cf10aefec601d34322b3ff8b');
            background-size: cover;
            display: inline-block;
        }
    </style>
</head>

<body>
    <h1>Bring wealth, bring wealth</h1>
    <div id="app"></div>

    <script>
        const app = document.getElementById('app');

        function addYuan() {
            const yuanContainer = document.createElement('DIV');
            yuanContainer.classList.add('yuan-container');
            yuanContainer.style.left = Math.random() * window.innerWidth + 'px';
            yuanContainer.style.animationDuration = 4 + Math.random() * 3 + 's';
            yuanContainer.style.fontSize = 20 + Math.random() * 30 + 'px';

            const yuanIcon = document.createElement('DIV');
            yuanIcon.classList.add('yuan-icon');
            yuanContainer.appendChild(yuanIcon);

            app.appendChild(yuanContainer);

            setTimeout(() => {
                yuanContainer.remove();
            }, 7000);
        }

        setInterval(addYuan, 500);
    </script>
</body>

</html>
  • @import url('fonts.googleapis.com/css?family=… Google Fonts 导入'Nanum Gothic'字体。
  • body:设置网页背景颜色和渐变,以及字体和布局。
  • h1:设置标题的颜色、阴影和大小。
  • .yuan-container:设置人民币图标容器的位置和动画。
  • @keyframes fall:定义名为 fall 的关键帧动画,使元素从页面顶部落下并逐渐消失。
  • .yuan-icon:设置人民币图标的尺寸和背景图片。
  • const app = document.getElementById('app');:获取页面中的div元素,用于添加人民币图标。
  • addYuan函数:创建一个新的div元素,设置其类、位置、动画持续时间,并添加人民币图标。然后将其添加到页面中,并在7秒后移除。
  • setInterval(addYuan, 500);:每500毫秒调用一次addYuan函数,添加新的人民币图标。