2
I was playing with a snippet I made just for fun and I came across a problem I can’t find a reason for your cause. Follow the snippet:
var i = 0;
setInterval(function() {
document.querySelectorAll('img').forEach(function(imagem) {
imagem.setAttribute('style', 'filter: hue-rotate('+ i +'deg)');
if(i < 360) { i++ } else { i = 0 }
});
}, 10);
When I run this snippet on a page with only one image and nothing else, taking the direct url of the image, it works exactly as I want. But if I play the same code on a normal web page that has images inside it, the rotation is jumping values absurdly, usually going through 3 values before going back to 0 degrees. That is, instead of every 0.1 seconds it increases 1 degree, as it should be, it increases 120 degrees every 0.1 second making the images blink. If I reduce the delay of the setInterval
, it is easy to see the change in degrees accompanying the Chrome flap. What is causing this, and how to resolve?
Grateful.
@Edit I did a test and some websites work normal while others give the error described above. An example page that gives the error described above would be the search for google images, or the Earth portal.
The problem is the scope of
i
. All images run a loop that incrementsi
. Therefore, the more images, the more increments ini
. What makes your animation so fast, depending on the amount of elements running this loop.– LipESprY
Thank you, it makes sense
– Máttheus Spoo
I’ll give you a solution. Wait a minute. ;)
– LipESprY
I intended to solve alone but can post that I mark the reply. Thanks again.
– Máttheus Spoo
I won’t even answer. Colleague @bio gave Submit before! ;D
– LipESprY
Thank you both anyway
– Máttheus Spoo
@Lipespry, man, I hadn’t even seen your conversation, I was doing the tests and formulating the answer, rsrs, but if you want to answer, no problem!
– bio
@bio Of course not. 2 responses with the same solution has no logic. But I’m not bothered that you have answered before. I believe our goal is the same and has been achieved, regardless of who published the answer. ;D
– LipESprY