Skip to content
Advertisement

Replace content of a div on hover with fadeIn and fadeOut

I am trying to replace the content of a div when user hovers on it. I have created two div‘s. The foreground div is the default and on hover action I want to hide the foreground and show background div.

JavaScript
JavaScript
JavaScript

However, nothing happens… What am I missing here?

Advertisement

Answer

The issue is because you’re using next(), which is looking for siblings, instead of find() which looks for children.

Note in the example below that I also added calls to stop() so that the flickering, which occurs when the event is called multiple times as the DOM updates, is avoided.

JavaScript
JavaScript
JavaScript

However it’s worth noting that you should use CSS where possible as it performs much better than JS. To do this you can hook :hover states to the .card-text and transition the child elements based on their opacity. Try this:

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