英文:
How do I make a rainbow button using Vanilla Javascript Client
问题
所以,我正在为一个项目制作一个面板...我想制作一个JavaScript版本的彩虹按钮,不是渐变的,它会平滑地改变颜色,就像彩虹LED灯一样。
例如,我们有一个名为 "set" 的变量。
如果 "set" 等于 true,那么按钮就是彩虹的。否则,它就是普通的。
我已经尝试了各种方法,但没有任何代码可以正常工作。
英文:
So, I'm making a panel for a project.. and I'd like to make a javascript version of a rainbow button, not gradient, it would smoothly change colors, like a rainbow LED light.
For example, we have a variable named "set"
If set equals true, then the button is rainbow. Otherwise, its normal.
I've been trying everything and have no luck with any code working.
答案1
得分: -1
以下是您要翻译的代码部分:
let rainbowButton = document.getElementById("rainbow-button");
function toggleLight() {
if (rainbowButton.classList.contains("rainbow-light"))
rainbowButton.classList.remove("rainbow-light")
else
rainbowButton.classList.add("rainbow-light")
}
#rainbow-button {
border: none;
border-radius: 8px;
background-color: gray;
padding: 8px;
}
.rainbow-light {
animation: rainbow-animation 2.5s linear;
animation-iteration-count: infinite;
}
@keyframes rainbow-animation {
100%, 0% {
background-color: rgb(255, 0, 0);
}
8% {
background-color: rgb(255, 127, 0);
}
16% {
background-color: rgb(255, 255, 0);
}
25% {
background-color: rgb(127, 255, 0);
}
33% {
background-color: rgb(0, 255, 0);
}
41% {
background-color: rgb(0, 255, 127);
}
50% {
background-color: rgb(0, 255, 255);
}
58% {
background-color: rgb(0, 127, 255);
}
66% {
background-color: rgb(0, 0, 255);
}
75% {
background-color: rgb(127, 0, 255);
}
83% {
background-color: rgb(255, 0, 255);
}
91% {
background-color: rgb(255, 0, 127);
}
}
<input id="rainbow-button" type="button" onclick="toggleLight()" value="Rainbow button">
请注意,这段代码是用于创建一个带有彩虹光效果的按钮的示例,通过JavaScript和CSS实现。
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let rainbowButton = document.getElementById("rainbow-button");
function toggleLight() {
if (rainbowButton.classList.contains("rainbow-light"))
rainbowButton.classList.remove("rainbow-light")
else
rainbowButton.classList.add("rainbow-light")
}
<!-- language: lang-css -->
#rainbow-button {
border: none;
border-radius: 8px;
background-color: gray;
padding: 8px;
}
.rainbow-light {
animation: rainbow-animation 2.5s linear;
animation-iteration-count: infinite;
}
@keyframes rainbow-animation {
100%, 0% {
background-color: rgb(255, 0, 0);
}
8% {
background-color: rgb(255, 127, 0);
}
16% {
background-color: rgb(255, 255, 0);
}
25% {
background-color: rgb(127, 255, 0);
}
33% {
background-color: rgb(0, 255, 0);
}
41% {
background-color: rgb(0, 255, 127);
}
50% {
background-color: rgb(0, 255, 255);
}
58% {
background-color: rgb(0, 127, 255);
}
66% {
background-color: rgb(0, 0, 255);
}
75% {
background-color: rgb(127, 0, 255);
}
83% {
background-color: rgb(255, 0, 255);
}
91% {
background-color: rgb(255, 0, 127);
}
}
<!-- language: lang-html -->
<input id="rainbow-button" type="button" onclick="toggleLight()" value="Rainbow button">
<!-- end snippet -->
You don't really need JS for this. This type of light can be done using CSS animations. If you want to control when the light is on/off you can just add/remove CSS class of your button.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论