繁体   English   中英

将 CSS 样式更改为 Div 的主体颜色

[英]Change Body Color on a CSS Style as Div

我是 HTML 和 CSS(学生)的新手,我在这里遇到了一个问题。

单击具有 CSS Animation 的元素时,我想更改正文背景颜色。这可能吗? 谢谢!

我试图将火箭设置为按钮,但它只改变了火箭的颜色。 我想让它改变身体的背景颜色。

 body { background: radial-gradient(circle at bottom, navy 0, black 100%); height: 100vh; overflow: hidden; }.orbit { height: 450px; width: 650px; border-radius: 50%; position: absolute; margin: auto; left: 500px; bottom: 300px; animation: spin 5s infinite linear; } @keyframes spin { 100% { transform: rotate(360deg) } }.rocket { background-color: #fafcf7; height: 50px; width: 25px; border-radius: 50% 50% 0 0; position: relative; left: 11px; top: 115px; }.rocket:before { position: absolute; content: ""; background-color: #39beff; height: 20px; width: 55px; z-index: -1; border-radius: 50% 50% 0 0; right: -15px; bottom: 0; }.rocket:after { position: absolute; content: ""; background-color: #39beff; height: 4px; width: 15px; border-radius: 0 0 2px 2px; bottom: -4px; left: 4.3px; }.window { position: relative; height: 10px; width: 10px; background-color: #151845; border: 2px solid #b8d2ec; border-radius: 50%; top: 15px; left: 5px; }
 <div class="orbit"> <button class="rocket"></button> <div class="window"></div> </div>

为此,您需要了解 JavaScript。使用此示例代码并尝试自己做更多事情。

 const rocket = document.getElementById("rocket") rocket.addEventListener('click', (e) => { const randomColor = Math.floor(Math.random()*16777215).toString(16); document.body.style = `background: radial-gradient(circle at bottom, #${randomColor} 0, black 100%);` })
 body { background: radial-gradient(circle at bottom, navy 0, black 100%); height: 100vh; overflow: hidden; }.orbit { height: 450px; width: 650px; border-radius: 50%; position: absolute; margin: auto; left: 500px; bottom: 300px; animation: spin 10s infinite linear; } @keyframes spin { 100% { transform: rotate(360deg) } }.rocket { background-color: #fafcf7; height: 50px; width: 25px; border-radius: 50% 50% 0 0; position: relative; left: 11px; top: 115px; cursor: pointer; }.rocket:before { position: absolute; content: ""; background-color: #39beff; height: 20px; width: 55px; z-index: -1; border-radius: 50% 50% 0 0; right: -15px; bottom: 0; }.rocket:after { position: absolute; content: ""; background-color: #39beff; height: 4px; width: 15px; border-radius: 0 0 2px 2px; bottom: -4px; left: 4.3px; }.window { position: relative; height: 10px; width: 10px; background-color: #151845; border: 2px solid #b8d2ec; border-radius: 50%; top: 15px; left: 5px; }
 <div class="orbit"> <button id="rocket" class="rocket"></button> <div class="window"></div> </div>

问题未解决?试试以下方法:

将 CSS 样式更改为 Div 的主体颜色

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM