英文:
Blinking Background color in JavaScript
问题
I am trying to have a blinking background color between Red and White by writing 2 functions with a 3-millisecond interval, but unfortunately, it doesn't work with my set element class named "Bad".
Can someone help me with what I am doing wrong?
Code is below:
function badBG() {
var colors = ['Red', 'White'];
for (i = 0; i < 2; i++) {
colorBG = colors[Math.floor(Math.random() * 2)];
// console.log(colorBG)
}
return colorBG;
}
function blinkBG() {
blink = badBG();
document.getElementsByClassName('Bad').style.backgroundColor = blink;
}
setInterval("blinkBG()", 300);
All are straight to the point functions and CSS methods.
英文:
I am trying to have a blinking background color between Red and Wite by writing 2 functions with 3milli-seconds interval, but unfortunately, it doesnt work with my set element class named, "Bad".
Can someone help me what am I doing wrong?
Code is below:
function badBG(){
var colors = ['Red','White']
for(i=0; i<2; i++){
colorBG = colors[Math.floor(Math.random()*2)];
// console.log(colorBG)
}
return colorBG
}
function blinkBG(){
blink = badBG()
document.getElementsByClassName('Bad').style.backgroundColor = blink;
}
setInterval("blinkBG()",300);
All are straight to the point functions and css methods.
答案1
得分: 1
请注意,我将只为您翻译代码部分,如下所示:
.better {
padding: 30px;
animation: blink 300ms infinite;
}
@keyframes blink {
from {
background: white;
}
to {
background: red;
}
}
<div class="better">我很好</div>
英文:
Better use a CSS animation:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.bad{
padding:30px;
animation: blink 300ms infinite;
}
@keyframes blink{
from {
background: white;
}
to{
background: red;
}
}
<!-- language: lang-html -->
<div class="bad">I'm very bad</div>
<!-- end snippet -->
答案2
得分: 0
以下是您要翻译的内容:
"While I would recommend solely solving this on the CSS level as Alexander Nenashev showed, there is also a clean JavaScript solution to it.
You simply us setInterval
in combination with classList.toggle()
to toggle a class in CSS. Means by adding/removing it:
const BAD = document.querySelectorAll('.bad');
BAD.forEach(element => {
setInterval(function() {
element.classList.toggle('bg-red');
}, 300)
})
.bad{
padding:30px;
}
.bg-red {
background-color: red;
}
"
英文:
While I would recommend solely solving this on the CSS level as Alexander Nenashev showed, there is also a clean JavaScript solution to it.
You simply us setInterval
in combination with classList.toggle()
to toggle a class in CSS. Means by adding/removing it:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const BAD = document.querySelectorAll('.bad');
BAD.forEach(element => {
setInterval(function() {
element.classList.toggle('bg-red');
}, 300)
})
<!-- language: lang-css -->
.bad{
padding:30px;
}
.bg-red {
background-color: red;
}
<!-- language: lang-html -->
<div class="bad">I'm very bad</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论