6
Good evening guys, I’m having a little challenge here and so far I haven’t managed to get the groove, if anyone can help me I would be very grateful:
I am trying to put 2 gifs allocated in the same place of the page, as follows:
loop{
1-page load
2-load gif1
3- delay 6000ms
4-remove gif1
5- loading gif2
6- delay 6000ms
7-remove gif2
}
Thus in eternal loop.
I managed to put the 2 banners to disappear and appear, the problem is that disappearing (fade out), the gif keeps changing the frames, so when it comes back (fade in), it is in the middle of some frame, and then it is out of sync...
I searched here and it seems that I would have to remove the src attribute from the img tag, but I don’t know how to put this in my function...
Follows my code:
$( window ).load(function() {
var $elem = $('#bannertoppage .bannerp'), l = $elem.length, i = 0;
function go() {
$elem.eq(i % l).fadeIn(100, function() {
$elem.eq(i % l).fadeOut(100, function(){
go();
});
i++;
}).delay(6200)
}
go();
});
Thanks in advance!
(solved) For those who need it, here is the final code:
html:
<div class="gif-wrapper">
<div id="0" class="gif-container">
<img class="gif-change" src="https://upload-assets.vice.com/files/2015/12/31/1451587321dj.gif">
</div>
<div id="1" class="gif-container">
<img class="gif-change" src=''>
</div>
</div>
Jquery:
var gifs = [{src:'http://seusite.com.br/seugif1.gif', delay: 6000}, {src: 'http://seusite.com.br/seugif2.gif', delay: 6000}];
var totalGifs = gifs.length;
var gifShow = 0;
$('.gif-wrapper .gif-container:gt(0)').hide();
setTimeout(change_gif, gifs[gifShow].delay, gifShow);
function change_gif(gifShow) {
$('.gif-wrapper .gif-container').hide();
gifShow++;
if(gifShow == totalGifs) gifShow = 0;
$('.gif-wrapper #' +gifShow+ ' img.gif-change').prop('src', gifs[gifShow].src+ '?'+ new Date().getTime());
setTimeout(change_gif, gifs[gifShow].delay, gifShow);
$('.gif-wrapper #' +gifShow).show();
}
And @Miguel jsfiddle https://jsfiddle.net/7e6kkL2a/5/
ABÇ!
This other question can also help you: http://answall.com/q/6457/73 (you can build a spritesheet with just the two gifs or simply replace the spritesheet if you have 2 of them).
– Luiz Vieira
@Luizvieira, it seems to me even duplicated because the answers there show how to solve this problem too.
– Sergio
@Sergio I briefly considered this possibility, but I concluded that it is not duplicated because this question effectively deals with the exchange of one "object" for another (so being a gif wouldn’t matter so much, and an answer here, when focused on that aspect, could help other people with similar needs).
– Luiz Vieira