我在 div 中有一个无限滚动的背景图像,如下面的代码片段所示。
这背后的想法是,它是一个我可以改变速度的时间线,即。正常速度、快速、慢速甚至反向。使用 javascript,我可以成功更改 div 上的“速度等级”,并且图像将成功更新为我希望的速度和方向。
但是我的问题是,当我更改类时,我会丢失图像的相对 translate3d 位置,因此当类更改时,背景图像位置会重置,然后新的动画开始。
我尝试纯粹从CSS的 Angular 来解决这个问题,通过尝试完全交换类,以及全职运行单个类并将所选动画应用为附加类,但是我我无法弄清楚如何在应用新类时保持相对背景位置。
交换类时如何保持背景图像位置,以免图像看起来从一个位置跳到另一个位置?
.timeline_frame {
width: 100%;
min-width: 1500px;
background-color: #444;
height: 250px;
left: 0;
right: 0;
margin: auto auto;
position: relative;
}
.timeline {
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto auto;
width: 1500px;
height: 200px;
}
.timeline_container {
width: 1500px;
height: 200px;
overflow: hidden;
position: relative;
}
.timeline_background {
background: url("https://www.aidanwardman.com/timeline-bg.png") repeat-x;
background-color: rgba(0, 0, 0, 0.5);
width: 3000px;
height: 200px;
vertical-align: bottom;
}
.timeline_speed_normal {
animation: slide 20s linear infinite;
}
.timeline_speed_slow {
animation: slide 40s linear infinite;
}
.timeline_speed_reverse {
animation: slide-reverse 2s linear infinite;
}
@keyframes slide {
0% {
transform: translate3d(0, 0, 0);
}
100% {
transform: translate3d(-1500px, 0, 0);
}
}
@keyframes slide-reverse {
0% {
transform: translate3d(-1500px, 0, 0);
}
100% {
transform: translate3d(0, 0, 0);
}
}
.button_frame {
left: 0;
right: 0;
margin: auto;
width: 400px;
padding: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="button_frame">
<button id="forward-normal">Forward Normal</button>
<button id="forward-slow">Forward Slow</button>
<button id="reverse">Reverse</button>
</div>
<div class="timeline_frame">
<div class="timeline">
<div class="timeline_container">
<div class="timeline_background timeline_speed_normal"></div>
</div>
</div>
</div>
<script>
$("#forward-normal").click(function() {
$(".timeline_background").removeClass("timeline_speed_fast timeline_speed_slow timeline_speed_reverse").addClass("timeline_speed_normal");
});
$("#forward-slow").click(function() {
$(".timeline_background").removeClass("timeline_speed_fast timeline_speed_normal timeline_speed_reverse").addClass("timeline_speed_slow");
});
$("#reverse").click(function() {
$(".timeline_background").removeClass("timeline_speed_fast timeline_speed_slow timeline_speed_normal").addClass("timeline_speed_reverse");
});
</script>
最佳答案
我会使用过渡而不是动画来模拟这样的事情:
.timeline_frame {
width: 100%;
min-width: 1500px;
background-color: #444;
height: 250px;
left: 0;
right: 0;
margin: auto auto;
position: relative;
}
.timeline {
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto auto;
width: 1500px;
height: 200px;
}
.timeline_container {
width: 1500px;
height: 200px;
overflow: hidden;
position: relative;
}
.timeline_background {
background: url("https://www.aidanwardman.com/timeline-bg.png") repeat-x;
background-color: rgba(0, 0, 0, 0.5);
width: 3000px;
height: 200px;
vertical-align: bottom;
transition:10s all;
}
.timeline_speed_normal {
transform: translate3d(-1500px, 0, 0);
}
.timeline_speed_slow {
transition:30s all;
transform: translate3d(-1501px, 0, 0);
}
.timeline_speed_reverse {
transform: translate3d(0, 0, 0);
}
.button_frame {
left: 0;
right: 0;
margin: auto;
width: 400px;
padding: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="button_frame">
<button id="forward-normal">Forward Normal</button>
<button id="forward-slow">Forward Slow</button>
<button id="reverse">Reverse</button>
</div>
<div class="timeline_frame">
<div class="timeline">
<div class="timeline_container">
<div class="timeline_background"></div>
</div>
</div>
</div>
<script>
$("#forward-normal").click(function() {
$(".timeline_background").removeClass("timeline_speed_fast timeline_speed_slow timeline_speed_reverse").addClass("timeline_speed_normal");
});
$("#forward-slow").click(function() {
$(".timeline_background").removeClass("timeline_speed_fast timeline_speed_normal timeline_speed_reverse").addClass("timeline_speed_slow");
});
$("#reverse").click(function() {
$(".timeline_background").removeClass("timeline_speed_fast timeline_speed_slow timeline_speed_normal").addClass("timeline_speed_reverse");
});
</script>
关于css - 交换类时保持 CSS Translate3d 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53454162/