I am trying to achieve a CSS only slider.
When hover
ing left and right arrows, the slider has to slide. Of course.
I tried something using animation-play-state
, animation-fill-mode
(to keep the positions) and animation-direction
but I'm not able to fully make it work.
- Starting with
animation-play-state: paused
,hover
ing the arrows changes it torunning
. - On
hover
of the right arrow, everything is fine. We can hover, leave, hover again. - But, as soon as I
hover
the left arrow (that changes theanimation-direction
toreverse
), it's broken.
Simplified snippet:
.wrapper {
overflow: hidden;
position: relative;
width: 500px;
}
.arrows {
z-index: 2;
position: absolute;
top: 0;
height: 100%;
background: #ddd;
opacity: 0.66;
}
.arrows:hover {
opacity: 1;
}
.arrow-l {
left: 0;
}
.arrow-r {
right: 0;
}
.sliding {
height: 160px;
width: 2000px;
background: linear-gradient(to bottom right, transparent 49.9%, gray 50.1%);
animation: slide 2s linear;
animation-play-state: paused;
animation-fill-mode: both;
}
.arrows:hover~.sliding {
animation-play-state: running;
}
.arrow-l:hover~.sliding {
animation-direction: reverse;
}
@keyframes slide {
0% {
transform: translate(0px, 0);
}
100% {
transform: translate(-1500px, 0);
}
}
<div class="wrapper">
<div class="arrows arrow-l">[ ← ]</div>
<div class="arrows arrow-r">[ → ]</div>
<div class="sliding"></div>
</div>
See Question&Answers more detail:os