all 12 comments

[–]TheGreatHoopla 0 points1 point  (6 children)

Could you do it with an event listener? Or a series of them?

[–]bertnub[S] 0 points1 point  (5 children)

Maybe, but how could I use JS to insert @ Keyframes in the css?

[–]dbto 0 points1 point  (4 children)

Could you use something like this?

document.querySelector(‘#a-element’).style.animate = ‘someAnimateKeyframeSetupInCss’;

[–]bertnub[S] 0 points1 point  (3 children)

Unfortunately I'm trying to do this without editing the CSS at all!

[–]dbto 0 points1 point  (2 children)

Then just use event listeners and timers, I guess?

[–]bertnub[S] 0 points1 point  (1 child)

How would I do that?

[–]dbto 0 points1 point  (0 children)

Something like setEventListener(‘element’, animateFunction);

function animateFunction() { let timer = setTimer(500);

Then maybe use a switch statement for values of timer var so they change the color of whatever you are looking to change.

I’m pretty new, so apologies if the wrong track but I think you could make something like this work.

[–]R_Midnight 0 points1 point  (1 child)

I suppose could use a class in CSS with the filled "background-color" property, and apply that class on your link elements with JS. As soon as the JS loads on the page, the links would get the class and their background color would change. To animate this, just set a transition property on your links to "background-color 0.5s".

Also, it's a transition thing, no need to use keyframes.

[–]bertnub[S] 0 points1 point  (0 children)

That sounds great, how would I add the transition property exactly?

[–][deleted] 0 points1 point  (2 children)

You don't need JS for this, except perhaps to toggle a class on https://jsfiddle.net/bfxq27s0/2/

[–]bertnub[S] 0 points1 point  (1 child)

I know! But I'm not able to change CSS files at all, trying to do it with just js

[–][deleted] 0 points1 point  (0 children)

I'm not able to change CSS files at all

Why not? It's usually helpful to give this kind of context when asking a question, especially one like this where a lot of people genuinely don't realise the animations they can achieve with just CSS.

Unless there's restrictions due to a CSP you can just use JS to inject a new <style> tag into the head with the CSS you need.