I have the code responsible for the fact that when you hover over an element, the yellow background animates from top to bottom.
How can I make an animation so that the yellow background slides down when the cursor moves off the element?
I tried using the hover animation again but it doesn’t work…
<div class="box">test</div>
.box:hover{
background-position:0 -100%;
}
.box{
height:150px; padding:30px;
background: #ffffff
background:-webkit-gradient(linear, left top, right top, color-stop(50%, #fff), color-stop(50%, #FFE606));
background:-webkit-linear-gradient(bottom, #fff 50%, #FFE606 50%);
background:-moz-linear-gradient(bottom, #fff 50%, #FFE606 50%);
background:linear-gradient(to bottom, #fff 50%, #FFE606 50%);
background-size:100% 200%;
background-position:0 0;
-webkit-transition:background-position .3s ease-out;
-moz-transition:background-position .3s ease-out;
transition:background-position .3s ease-out;
}
2
Answers
In this modified CSS, the :not(:hover) pseudo-class is used to target the .box element when it’s not being hovered over. By setting the background-position to 0 100%, the yellow background will slide down smoothly when the cursor moves off the element.
Add tag in your element and use
has()
selector.For this sample I use span tag. So when you hover on the test element using has() selector, it will activate the style.
EDIT: You can use javascript to stop the animation.