[英]Why first variation of function works but second doesn't?
Function 1. Works perfectly. 功能1.完美运作。 Makes a div flicker.
使div闪烁。
function makeItFlicker(){
let y = document.getElementById(x).style.backgroundColor;
if (y=="blue"){
document.getElementById(x).style.backgroundColor = "#557a95";
}
else {
document.getElementById(x).style.backgroundColor = "blue";
}
}
setInterval(makeItFlicker, 500);
This doesn't. 事实并非如此。 I just changed number to it's hashtag value.
我只是将数字更改为它的标签值。
function makeItFlicker(){
let y = document.getElementById(x).style.backgroundColor;
if (y=="#90acc0"){
document.getElementById(x).style.backgroundColor = "#557a95";
}
else {
document.getElementById(x).style.backgroundColor = "#90acc0";
}
}
setInterval(makeItFlicker, 500);
Anyone has an idea why? 任何人都知道为什么?
Browsers handle colors differently. 浏览器处理颜色的方式不同 Sometimes it's HEX, sometimes it's RGBA, RGB, will this change in the future browser updates?
有时它是HEX,有时它是RGBA,RGB,这将在未来的浏览器更新中发生变化吗? who knows and who cares.
谁知道谁在乎谁 You can store the interpreted color into a
data-*
attribute... 您可以将解释的颜色存储到
data-*
属性中......
But hey, why don't we try first a pure CSS solution using animation keyframes 但是,嘿,为什么我们不首先尝试使用动画关键帧的纯CSS解决方案
#x { padding: 50px; background: blue; animation: blink 0.5s infinite; } @keyframes blink { to {background: #557a95;} }
<div id="x"></div>
If you really want JS... than you could store the current background inside a data-*
attribute, and than simply toggle back and forth the data and background color values: 如果你真的想要JS ...而不是将当前背景存储在
data-*
属性中,而不是简单地来回切换数据和背景颜色值:
const blinkBackground = el => { const blink = el.getAttribute('data-blink'); // get current data... el.setAttribute('data-blink', el.style.background); // (store current bg value) el.style.background = blink; // ...and set it as background } document.querySelectorAll('[data-blink]').forEach(el => setInterval(blinkBackground.bind(null, el), 500) );
[data-blink] {padding: 10px; margin: 10px; background: blue;}
<div data-blink="#557a95"></div> <div data-blink="red"></div> <div data-blink="hsla(100, 50%, 80%, 0.8)"></div>
You can also store an array of colors: 您还可以存储一系列颜色:
const blinkBackground = el => { const c = JSON.parse(el.getAttribute('data-blink')); el.style.background = c[0]; el.setAttribute('data-blink', JSON.stringify(c.reverse())); } document.querySelectorAll('[data-blink]').forEach(el => setInterval(blinkBackground.bind(null, el), 500) );
[data-blink] {padding: 10px; margin: 10px;}
<div data-blink='["#557a95", "blue"]'></div> <div data-blink='["rgb(0,0,0)", "gold"]'></div> <div data-blink='["orange", "hsla(100, 50%, 80%, 0.8)"]'></div>
because style.backgroundColor
returns the rgb value not the string value of the color. 因为
style.backgroundColor
返回rgb值而不是颜色的字符串值。
so in the second function y will have a value like this rgb(255, 241, 35)
所以在第二个函数中y将有一个像这样的值
rgb(255, 241, 35)
you can use this method to convert the results to the hex string value 您可以使用此方法将结果转换为十六进制字符串值
function rgbToHex(str) {
let values = str.replace('rgb(', '').replace(')', '').split(',');
let r = values[0], g = values[1], b = values[2];
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
"Different browsers behave differently with color values" @Pointy
“不同的浏览器在颜色值方面表现不同”@Pointy
I've tested that on both chrome and safari, and it returns the same value, bus sure we need to make sure its working cross browsers 我已经在chrome和safari上测试了它,它返回相同的值,总线确定我们需要确保它的工作交叉浏览器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.