【发布时间】:2018-07-07 08:06:18
【问题描述】:
我正在尝试实现仅 CSS 的滑块。
当hovering 左右箭头时,滑块必须滑动。当然。
我尝试使用animation-play-state、animation-fill-mode(以保持位置)和animation-direction,但我无法完全发挥作用。
- 以
animation-play-state: paused开头,hover箭头将其更改为running。 - 在右箭头的
hover上,一切正常。我们可以悬停、离开、再次悬停。 - 但是,只要我
hover左箭头(将animation-direction更改为reverse),它就坏了。
简化的 sn-p:
.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>
有人可以帮助我了解正在发生的事情,并纠正这种不良行为吗?
【问题讨论】:
-
改变方向会改变整个动画,不会简单地通过保持当前状态来改变当前方向
-
嘿@TemaniAfif,谢谢。你知道纠正它的方法吗?
-
当你的动画停止时,它就停止了。它不会通过反转方向重新开始。当你把它设置为无限时,它会起作用:jsfiddle.net/eL6ygf51/8 但是当然,动画将是无限的,所以你必须找到一种方法来调整它。
标签: css css-animations css-transforms