<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{margin: 0;padding: 0;} div{ width: 100px; height: 100px; position: absolute; } </style> </head> <body> <div></div> <script> var dropY = 0 //自定义的值 var style = document.styleSheets[0]; amt=[] for(s=0;s<10;s++){ var amtw = ["@keyframes run"+s+" {0% {transform: translate(0px,0px);}100% {transform: translate("+dropY+"px,"+dropY+"px);}}"]; dropY+=100 amt.push(amtw) } for(var i = 0; i<amt.length;i++){ div=document.createElement('div') div.style.backgroundColor='rgb('+ color(0,255) +','+ color(0,255) +','+ color(0,255) +')' div.style.animation='run'+i+' '+i+'s forwards' document.body.appendChild(div) style.insertRule(amt[i],i); } function color(start,end){ return Math.floor(Math.random()*(end-start)+start); } </script> </body> </html>