雪花代码-html版

雪花代码

动画效果

代码

javascript 复制代码
<!DOCTYPE html>

<html>

<head>

<style>

body {

background-color: #000000;

}

.snowflake {

position: absolute;

font-size: 10px;

color: #FFFFFF;

text-shadow: 1px 1px 1px #000000;

user-select: none;

}

</style>

</head>

<body>

<script>

function random(min, max) {

return Math.floor(Math.random() * (max - min + 1)) + min;

}

function Snowflake() {

this.characters = "❄️";

this.x = random(0, window.innerWidth);

this.y = random(-200, -100);

this.speed = random(1, 5);

this.element = document.createElement("span");

this.element.classList.add("snowflake");

this.element.innerHTML = this.characters;

document.body.appendChild(this.element);

}

Snowflake.prototype.update = function() {

this.y += this.speed;

this.element.style.top = this.y + "px";

this.element.style.left = this.x + "px";

if (this.y > window.innerHeight) {

this.y = random(-200, -100);

this.x = random(0, window.innerWidth);

}

};

var snowflakes = [];

for (var i = 0; i < 100; i++) {

snowflakes.push(new Snowflake());

}

setInterval(function() {

snowflakes.forEach(function(snowflake) {

snowflake.update();

});

}, 50);

</script>

</body>

</html>
相关推荐
费曼学习法1 分钟前
Vue 响应式系统源码级剖析:从 Object.defineProperty 到 Proxy
javascript·vue.js
前端那点事2 分钟前
Vue十万条数据渲染无卡顿!3种工业级方案(附可复制代码+避坑指南)
前端·vue.js
神奇小汤圆17 分钟前
快手一面:为什么要求用Static来修饰ThreadLocal变量?
javascript
tenggouwa18 分钟前
16GB Mac 同时开 3 个 Cursor 拯救我的mac
前端·后端
天天打码22 分钟前
从 Rolldown 到 Oxc:前端工具链正在全面 Rust 化
开发语言·前端·rust
zubylon22 分钟前
前端 RAG:把文档检索接到聊天页
前端·人工智能·算法
行业研究员26 分钟前
HTML头部元信息避坑指南大纲
javascript
犹豫的果冻布丁28 分钟前
OpenSpec 完全中文教程:AI 规范驱动开发入门与实战
前端·后端
Beginner x_u29 分钟前
前端八股整理总索引|JS/TS、HTML/CSS、Vue、浏览器、工程化与手写题
前端·javascript·html
Cobyte36 分钟前
10.响应式系统演进:通过位运算优化动态依赖收集(Vue3.2)
前端·javascript·vue.js