|
|

楼主 |
发表于 2023-7-8 19:56
|
显示全部楼层
测试代码
- <style>
- #mydiv {
- margin: 20px auto;
- width: 700px;
- height: 460px;
- border: 1px solid purple;
- overflow: hidden;
- position: relative;
- }
- li-zi {
- position: absolute;
- animation: rot 6s infinite linear;
- }
- @keyframes rot { to { transform: rotate(2turn); } }
- </style>
- <div id="mydiv"></div>
- <script>
- class Lizi {
- constructor(pa) {
- this.pa = pa;
- this.left = 10;
- this.top = 10;
- this.xstep = 1;
- this.ystep = 1;
- this.ele = document.createElement('li-zi');
- }
- creating() {
- this.ele.style.cssText = `
- left: ${this.left}px;
- top: ${this.top}px;
- `;
- this.pa.appendChild(this.ele);
- this.moving();
- }
- moving() {
- this.left += this.xstep;
- this.top += this.ystep;
- if(this.left >= this.pa.offsetWidth) this.left = - this.ele.offsetWidth;
- if(this.top >= this.pa.offsetHeight) this.top = -this.ele.offsetHeight;
- this.ele.style.left = this.left + 'px';
- this.ele.style.top = this.top + 'px';
- requestAnimationFrame(this.moving.bind(this));
- }
- }
- let stepAr = [0.2,0.5,0.8,1,1.2,1.5];
- let txtAr = ['\u{1F338}','\u{1F4AE}','\u{1F33A}','\u{1F3F5}','\u2740'];
- Array.from({length: 50}).forEach((element) => {
- let xIdx = Math.floor(Math.random() * stepAr.length),
- yIdx = Math.floor(Math.random() * stepAr.length),
- txtIdx = Math.floor(Math.random() * txtAr.length);
- element = new Lizi(mydiv);
- element.left = Math.ceil(Math.random() * (mydiv.offsetWidth - 50));
- element.top = Math.ceil(Math.random() * (mydiv.offsetHeight - 50));
- element.xstep = stepAr[xIdx];
- element.ystep = stepAr[yIdx];
- element.bg = 'none';
- element.ele.innerText = txtAr[txtIdx];
- element.creating();
-
- element.ele.style.fontSize = 10 + Math.ceil(Math.random() * 20) + 'px';
- element.ele.style.color = '#' + Math.random().toString(16).substr(-6);
- });
- </script>
复制代码
|
|