var gradient = "linear-gradient( 109.6deg, rgba(0,0,0,1) 11.2%, rgba(11,132,145,1) 91.1% )"; window.onload = function () { const duration = 1500 const style = document.createElement('style'); document.head.appendChild(style); style.sheet.insertRule(` body::after { opacity: 0; /* 让渐变背景慢慢显现 */ background-image: ${gradient}; transition: opacity ${duration}ms linear; position: absolute ; top: 0; left: 0; width: 100%; height: 100%; background-color: yellow; content:""; } `, style.sheet.cssRules.length); setTimeout(() => { document.body.offsetHeight; const style = document.createElement('style'); document.head.appendChild(style); style.sheet.insertRule(` body::after { opacity: 1; /* 让渐变背景慢慢显现 */ content:""; z-index: -1; } `, style.sheet.cssRules.length); console.log(2) }, 500) setTimeout(() => { document.body.style.backgroundImage = gradient const style = document.createElement('style'); document.head.appendChild(style); style.sheet.insertRule(` body::after { display: none; } `, style.sheet.cssRules.length); console.log("done") }, duration * 2) };