I have a div, and I have a gif as a background (a computer entering like a car). This gif is placed when the scrollTop
is at specific number... If the user keeps scrolling I switch the background for another gif (the computer leaving). Here is the code for it:
if (($(window).width() > 1700 && vistaEstandar.scrollTop > 4286 && vistaEstandar.scrollTop < 5171) || ($(window).width() > 1700 && document.documentElement.scrollTop > 4286 && document.documentElement.scrollTop < 5171)) {
$('#section6').removeClass('monitorEstandarAnimacion');
$('#section6').addClass('monitorEstandarFinal');
}
else
{
$('#section6').removeClass('monitorEstandarAnimacion');
$('#section6').removeClass('monitorEstandarFinal');
}
Ok, now, the classes assigned are the ones that contain the backgrounds
.monitorEstandarAnimacion
{
background: url(../images/estandar/monitorEstandarEntrada.gif?q=) !important;
background-size: cover !important;
background-position: bottom !important;
width: 100%;
}
.monitorEstandarFinal
{
background: url(../images/estandar/monitorEstandarSalida.gif?q=) !important;
background-size: cover !important;
background-position: bottom !important;
width: 100%;
}
The switching of backgrounds works. However, when I scroll back and the class is added again, the gif should start over, but it doesn't. The background becomes the last frame of the gif automatically. It's like the gif only works one (it only loops once), but I want it to work once (as it was made) everytime the switching occurs
a very similar question to this is found here: Restart animated GIF as background-image
and that would be perfect if not for the fact that I am working with scroll, so when trying to implement what he says, even though the gif switch works like a charm, It reloads everytime the scroll happens, not just once as intended.
As you can see, in the if statement there is a range stablished. In that range, the gif is supposed to play once. I have noticed though, that the repeting of the gif with each scroll might occur because is constantly reading the value thus replacing it over again, but I only need it once
UPDATE
Here is the fiddle. Here Is what's happening... the idea is that when they are changed back again, they should replay: https://jsfiddle.net/lj_tang/qynvg44b/11/