Problem Description
I am trying to find a way to, whenever a mouse enters a div, for an image to appear gradually, and equivalently, disappear slowly when the mouse leaves.
function App() {
const imagePath = 'https://via.placeholder.com/100';
const [showImage, setShowImage] = React.useState(false);
return (
<div className="container"
onMouseEnter={() => setShowImage(true)}
onMouseLeave={() => setShowImage(false)}
style={ {backgroundImage: showImage ? `url(${imagePath})` : `none` }}>
</div>
)
}
ReactDOM.render(<App />, document.querySelector("#app"));
.container{
background-color: orange;
width: 100px;
height: 100px;
animation-duration: 0.75s;
transition: all 0.5s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="app"></div>
What I have tried
- I tried adding the
allprefix to the transition property in css, but that didn't work. - I tried looking online for solutions, but I can't seem to find how, and this makes me think that there is a particular way to implement this if it involves an image appearing/disappearing.
Does anyone know how I can solve this problem ?