Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animated gif starting on scroll

I have made 4 animated gifs and i want them to start by the user's scroll.

What i did is use this:

$(function () {
    var screen = $(".screen");
    $(window).scroll(function () {
        var scroll = $(window).scrollTop();

        if (scroll >= 1500) {
            screen.css('display','block');
        } else {
            screen.css('display','none');
        }
    });
});

I tried to display none and block the gifs by scroll and it works good, the problem is that the gif doesnt start from the beginning when i display block it, it starts from somewhere in the middle of the animation.

how can i make a gif start exactly from the first frame when i display block it?

Thanks very much!

like image 610
Danigoodw Avatar asked Sep 05 '25 03:09

Danigoodw


1 Answers

Your code does hide and show the images but it does not reload the image. Instead you should update your src attribute, or simple realod the same image.

$(this).attr("src",$(this).attr("src"))

One simple solution would be 

if (scroll >= 1500) {
   screen.attr('src',screen.attr('src')); // update its src, this reloads the img
} else {
   screen.css('display','none');
}
like image 73
niko Avatar answered Sep 08 '25 00:09

niko