给一段下雪的代码。。。

给一段下雪的代码。。。

<SCRIPT LANGUAGE="JavaScript1.2">

<!-- Begin
var no = 10; // snow number
var speed = 30; // smaller number moves the snow faster
var snowflake = "http://www.mambochina.net/modules/snow.png";

var ns4up = (document.layers) ? 1 : 0;  // browser sniffer
var ie4up = (document.all) ? 1 : 0;
var dx, xp, yp;    // coordinate and position variables
var am, stx, sty;  // amplitude and step variables
var i, doc_width = 800, doc_height = 600;
if (ns4up) {
doc_width = self.innerWidth;
doc_height = self.innerHeight;
} else if (ie4up) {
doc_width = document.body.clientWidth;
doc_height = document.body.clientHeight;
}
dx = new Array();
xp = new Array();
yp = new Array();
am = new Array();
stx = new Array();
sty = new Array();
for (i = 0; i < no; ++ i) {
dx[i] = 0;                        // set coordinate variables
xp[i] = Math.random()*(doc_width-50);  // set position variables
yp[i] = Math.random()*doc_height;
am[i] = Math.random()*20;         // set amplitude variables
stx[i] = 0.02 + Math.random()/10; // set step variables
sty[i] = 0.7 + Math.random();     // set step variables
if (ns4up) {                      // set layers
if (i == 0) {
document.write("<layer name=/"dot"+ i +"/" left=/"15/" ");
document.write("top=/"15/" visibility=/"show/"><img src=/"");
document.write(snowflake + "/" border=/"0/"></layer>");
} else {
document.write("<layer name=/"dot"+ i +"/" left=/"15/" ");
document.write("top=/"15/" visibility=/"show/"><img src=/"");
document.write(snowflake + "/" border=/"0/"></layer>");
   }
} else if (ie4up) {
if (i == 0) {
document.write("<div id=/"dot"+ i +"/" style=/"POSITION: ");
document.write("absolute; Z-INDEX: "+ i +"; VISIBILITY: ");
document.write("visible; TOP: 15px; LEFT: 15px;/"><img src=/"");
document.write(snowflake + "/" border=/"0/"></div>");
} else {
document.write("<div id=/"dot"+ i +"/" style=/"POSITION: ");
document.write("absolute; Z-INDEX: "+ i +"; VISIBILITY: ");
document.write("visible; TOP: 15px; LEFT: 15px;/"><img src=/"");
document.write(snowflake + "/" border=/"0/"></div>");
      }
   }
}
function snowNS() {  // Netscape main animation function
for (i = 0; i < no; ++ i) {  // iterate for every dot
yp[i] += sty[i];
if (yp[i] > doc_height-50) {
xp[i] = Math.random()*(doc_width-am[i]-30);
yp[i] = 0;
stx[i] = 0.02 + Math.random()/10;
sty[i] = 0.7 + Math.random();
doc_width = self.innerWidth;
doc_height = self.innerHeight;
}
dx[i] += stx[i];
document.layers["dot"+i].top = yp[i];
document.layers["dot"+i].left = xp[i] + am[i]*Math.sin(dx[i]);
}
setTimeout("snowNS()", speed);
}
function snowIE() {  // IE main animation function
for (i = 0; i < no; ++ i) {  // iterate for every dot
yp[i] += sty[i];
if (yp[i] > doc_height-50) {
xp[i] = Math.random()*(doc_width-am[i]-30);
yp[i] = 0;
stx[i] = 0.02 + Math.random()/10;
sty[i] = 0.7 + Math.random();
doc_width = document.body.clientWidth;
doc_height = document.body.clientHeight;
}
dx[i] += stx[i];
document.all["dot"+i].style.pixelTop = yp[i];
document.all["dot"+i].style.pixelLeft = xp[i] + am[i]*Math.sin(dx[i]);
}
setTimeout("snowIE()", speed);
}
if (ns4up) {
snowNS();
} else if (ie4up) {
snowIE();
}
// End -->
</script>
以下是一个简单的实现在网站上下雪代码: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Snowing Effect</title> <style> body { overflow: hidden; } #snow-canvas { position: absolute; top: 0; left: 0; right: 0; bottom: 0; z-index: 1; } </style> </head> <body> <canvas id="snow-canvas"></canvas> <script> var canvas = document.getElementById('snow-canvas'); var ctx = canvas.getContext('2d'); // 设置画布尺寸 canvas.width = window.innerWidth; canvas.height = window.innerHeight; // 雪花数量 var snowCount = 100; // 雪花数组 var snowflakes = []; // 创建雪花对象 function createSnowflake() { return { x: Math.random() * canvas.width, y: -10, r: Math.random() * 4 + 1, d: Math.random() * snowCount } } // 初始化雪花数组 for (var i = 0; i < snowCount; i++) { snowflakes.push(createSnowflake()); } // 绘制雪花 function drawSnowflake(snowflake) { ctx.beginPath(); ctx.arc(snowflake.x, snowflake.y, snowflake.r, 0, Math.PI * 2, false); ctx.fillStyle = "white"; ctx.fill(); } // 雪花下落 function snowfall() { // 清空画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 绘制每个雪花 for (var i = 0; i < snowCount; i++) { var snowflake = snowflakes[i]; snowflake.y += Math.cos(snowflake.d) + 1 + snowflake.r / 2; snowflake.x += Math.sin(snowflake.d) * 2; // 如果雪花落出画布,重新生成 if (snowflake.y > canvas.height) { snowflakes[i] = createSnowflake(); } drawSnowflake(snowflake); } // 递归调用 requestAnimationFrame(snowfall); } // 开始下雪 snowfall(); </script> </body> </html> ``` 你可以直接将上述代码复制到一个 HTML 文件中,然后在浏览器中打开该文件,就能看到网站下雪的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值