简体   繁体   English

为什么第一个功能的变化有效,但第二个没有?

[英]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.

相关问题 .filter(_function) for arrays——我不确定为什么我的过滤器/函数的第一个版本有效但第二个版本无效 - .filter(_function) for arrays -- I'm not sure why the first version of my filter/function works but the second one doesn't 首先总结,第二不-&gt;为什么? - First sums, second doesn't -> why? jQuery .on适用于第一个html,但不适用于第二个(相同格式) - JQuery .on works on first html but doesn't work on the second(same form) innerHTML 在第一次单击按钮时有效,但在第二次单击时无效 - innerHTML works on first button click but doesn't work on second click Array.map() 仅适用于第一个数组,但不适用于第二个数组。 有人可以解释为什么它不起作用并提供适当的解决方案 - Array.map( ) only works on First Array but not on the Second. Can someone please explain why it doesn't work and provide proper solution 为什么在首次加载网站时此JavaScript函数无法正确计算,但之后仍能正常工作? - Why doesn't this JavaScript function calculate correctly when the site is first loaded, but works fine afterwards? 为什么第一个代码有效而第二个代码无效? - why does the first code work and the second doesn't work? 为什么第二个代码与第一个代码的工作方式不同? - Why doesn't the second code work the same as the first code? JavaScript表单验证:为什么第一种有效,但第二种无效? - JavaScript form validation: Why does the first work but the second doesn't? 为什么我的第一个snippit工作,但我的第二个没有? - Why does my first snippit work, but my second doesn't?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM