|
|

楼主 |
发表于 2023-7-11 07:50
|
显示全部楼层
代码
- <style>
- #mydiv {
- margin: 20px auto;
- width: 740px;
- height: 500px;
- background: #333;
- overflow: hidden;
- cursor: pointer;
- position: relative;
- --state: running;
- }
- li-zi {
- position: absolute;
- background: linear-gradient(to bottom, transparent, snow);
- animation: drop var(--dur) var(--delay) infinite linear var(--state);
- }
- @keyframes drop { to { top: 100%; } }
- </style>
- <div id="mydiv"></div>
- <script>
- let canMove = true;
- //Lizi 类
- class Lizi {
- constructor(pa) {
- this.pa = pa;
- this.size = {x: 20, y: 20};
- this.pos = {x: 10, y: 10};
- this.ele = document.createElement('li-zi');
- };
- //创建粒子
- creating() {
- this.ele.style.cssText = `
- width: ${this.size.x}px;
- height: ${this.size.y}px;
- left: ${this.pos.x}px;
- top: ${this.pos.y}px;
- `;
- this.pa.appendChild(this.ele);
- };
- }
- //实例化类
- Array.from({length: 200}).forEach((element) => {
- element = new Lizi(mydiv);
- element.size = {
- x: 1 + Math.round(Math.random()),
- y: 8 + Math.random() * 30
- };
- element.pos = {
- x: Math.floor(Math.random() * mydiv.offsetWidth),
- y: 0
- };
- element.creating();
- element.ele.style.setProperty('--dur', 0.4 + Math.random() * 0.4 + 's');
- element.ele.style.setProperty('--delay', Math.random() * -0.8 + 's');
- });
- mydiv.onclick = () => {
- canMove = !canMove;
- mydiv.style.setProperty('--state', canMove ? 'running' : 'paused');
- console.log(canMove);
- }
- </script>
复制代码
|
|