html代码特效粒子,html5彩色方块粒子动画效果代码

这篇博客详细介绍了如何使用HTML5创建一个彩色方块粒子动画。通过创建元素碎片,设置元素位置和背景颜色,并利用数学函数实现粒子的动态移动,创造出视觉效果丰富的动画。代码中使用了requestAnimationFrame进行平滑动画更新,并通过sin函数计算粒子的新位置,为每个方块赋予不同的运动轨迹。
摘要由CSDN通过智能技术生成

特效描述:html5 彩色方块 粒子动画效果。粒子动画

代码结构

1. HTML代码

var df = document.createDocumentFragment();

var container = document.getElementById('container');

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

var x = i % 30;

var y = ~~ (i / 30);

var elm = document.createElement('div');

elm.className = 'box';

elm.style.top = (y * 10) + 'px';

elm.style.left = (x * 10) + 'px';

elm.style.backgroundColor = color(x%y+y%x);

df.appendChild(elm);

}

container.appendChild(df);

var cycle = 0;

var children = container.children;

run();

function run() {

requestAnimationFrame(run);

for (var i = 0, l = children.length; i < l; i++) {

var top = parseFloat(children[i].style.top);

var left = parseFloat(children[i].style.left);

children[i].style.top = top + Math.cos(cycle + (i % 30)) + 'px';

children[i].style.left = left + Math.sin(cycle + ~~ (i / 30)) + 'px';

cycle += 1e-4;

}

}

function color(i) {

var r = Math.floor( Math.sin(i) * 127 + 128 );

var g = Math.floor( Math.sin(i + 2) * 127 + 128 );

var b = Math.floor( Math.sin(i + 3) * 127 + 128 );

return 'rgb(' + r + ', ' + g + ',' + b + ')';

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值