Como cambiar los estilos de un pseudo elemento con Javascript // How to change the styles of a pseudo element with Javascript

Muchas veces te habrá tocado utilizar pseudo elementos con CSS y por alguna razón u circunstancia tenías que darle alguna animación requerida, desde la hoja de estilos se puede hacer, pero solo con pseudo clases; esto es sencillo, pero supongamos que uno desea que el efecto se aplique cuando se ejecute un evento.
Aquí se torna un poco más difícil de pensar cómo hacerlo, pero también es sencillo y te explicare una manera de logar esto.

Many times you will have had to use pseudo elements with CSS and for some reason or circumstance you had to give it some required animation, from the style sheet it can be done, but only with pseudo classes; this is straightforward, but suppose one wants the effect to be applied when an event is executed. Here it becomes a little more difficult to think how to do it, but it is also simple and I will explain a way to achieve this.

ps1.jpg
Las hojas de estilo te proporciona algo llamado variables “var()” en donde podemos almacenar valores y estilos que deseamos reutilizar con el mismo nombre en otras etiquetas, y esta misma es la ventaja para poder cambiar estilos ejecutando eventos desde javascript.

The style sheets provide you with something called “var()” variables where we can store values ​​and styles that we want to reuse with the same name in other tags, and this is the same advantage to be able to change styles by executing events from javascript.

ps2.jpg
Una vez que tienes tus variables definidas los unico que debes hacer es utilizar “setProperty”, con este método estaremos cambiando el valor de una variable en las hojas de estilos.

Once you have your variables defined, the only thing you have to do is use “setProperty”, with this method we will be changing the value of a variable in the style sheets.

ps3.jpg

Click!!!!!

ps4.jpg
Ahora tiene la mitad del ancho, porque solo cambie el valor de esa variable, pero gracias a javascript se puede hacer más dinámico y tener valores deseados.

Now it is half the width, because it only changed the value of that variable, but thanks to javascript it can be made more dynamic and have desired values.

ps5.jpg


ps6.jpg



0
0
0.000
3 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

0
0
0.000
avatar

Excelente para cambiar estilos desde Javascript sin tener que recorrer a propiedades como hover en CSS.

Saludos.

0
0
0.000
avatar

Es la única manera que encontré de alterar el css de un pseudo elemento porque al q
Ser pseudo no pueden ser alterados a través de un queryselector ni nada parecido

0
0
0.000