Skip to content
Advertisement

change opacity and animated that with react js

i started a simple project with react.in my project i have a paragraph and when mouse hover on paragraph (mouse enter event) a square appears under the paragraph and when hover out from paragraph(mouse leave event) that square disapear.but this occure so fast so i want changing this smoothly and i want use opacity and change that from 0 to 1 and reverse when my events occure.but I do not know what to do to change the opacity with animation in react.

this is my appjs

JavaScript

and this is my index.css

JavaScript

I would be very grateful if anyone could help me

Advertisement

Answer

Here is a method without using useState though. I don’t know if this part is important, but have a look at my sandbox

First of all you need a css class to define the opacity the method and how much time it will take. Also, your first square class should have opacity: 0, meaning non visible.

When mouse is over text, then you add the extra class to the element.

JavaScript

JavaScript

Updated answer: No need for ref

Just use the following code

JavaScript

along with the extra class i mentioned before

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement