hitode909の日記

趣味はマリンスポーツですの日記です

アニメーションGIFを最初から再生する

srcを一瞬別の画像にしてすぐにもとの画像に戻すとGIFを最初から再生できると教えてもらって,へえと思ったので実験.


https://gph.is/18VKkP7

ページ内に同じURLのGIFが表示されているときは最初に戻らないようだった.URLの末尾に?1とか?2とかダミーのパラメータをつけてURLを変えると回避できるけど,その枚数分ロードされるので通信量は上がってしまう.

(() => {
const gif = document.querySelector('#the-gif');
const originalSrc = gif.src;
const empty = 'https://cdn-ak2.f.st-hatena.com/images/fotolife/h/hitode909/20171101/20171101190546.gif';
let speed = 10;
const main = () => {
gif.src = empty;
window.requestAnimationFrame(() => {
gif.src = originalSrc;
});
speed *= 1.1;
if (speed > 5000) speed = 10;
setTimeout(main, speed);
};
main();
})();
view raw a.js hosted with ❤ by GitHub