I have an infinite scrolling background image in a div as you can see in the below snippet.
The idea behind this is that its a timeline that I can change the speed of, ie. Normal speed, Fast, Slow and even Reverse. Using javascript I can successfully change the "speed class" that I have on the div and the image will successfully update to the speed and direction that I wish it to go.
However my issue is that when I'm changing class, I lose the relative translate3d position of the image, so as the class changes, the background image location gets reset and then the new animation starts.
I've tried to address this a few ways purely from a css point of view, by trying a complete swap of classes, as well as running a single class full time and applying the selected animation as an additional class, however I am unable to figure out how to maintain the relative background position when the new class is applied.
How can I maintain the background images location when I swap class so that it doesn't look like the image is jumping from one position to another?
.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>
I would simulate such thing using transition instead of animation: