Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play a GIF on click (like 9GaG.com) with wordpress?

I need to implement this "gif player" in a wordpress site, because gif pages are up to 6mb, so performance is really crappy

I´ve read this Onclick play GIF image with jQuery and start from beginning also this How to play animated GIF from the beginning onclick function that were almost a solution but i dont know how this should be done with wordpress in every entry

9gag.com does it perfectly; shows a preview image , reproduces the gif onclick, and stop the gif if its clicked again. If clicked again, plays the gif from the start again

How can i accomplish this with wordpress?

like image 844
roquelage Avatar asked Jan 04 '14 02:01

roquelage


People also ask

Why are my GIFs not working WordPress?

The reason why your GIF is not working in WordPress is that when you upload a GIF to WordPress, it gets resized. Depending on your WordPress theme, any image or GIF you upload on your website gets resized into various sizes. In such cases, your GIFs also get resized.

Can you put GIFs in WordPress?

Can I Copy and Paste a GIF to WordPress? If the GIF you want to use is live on the internet, you can right-click on it and copy the image. Then, you can paste it onto a WordPress post or page.

Can a GIF be a featured image WordPress?

Not only you can use GIF in a text, but you can also use it as a featured image and thumbnail in WordPress.

How do I add a background image to a GIF in WordPress?

Click on the “Add Media” button above the editor in WordPress. Click on the Upload Files tab of the Insert Media window. You can click on the Select Files button or drag and drop the file into the media space in WordPress. Either of these options will give you a chance to upload your animated GIF.


2 Answers

If you don't want to pause, you can easily REPLAY the gif file. Just trigger on the click and reload the gif into the image tag.

your_image= document.getElementById or however you want to grab the reference to the image. then set an event listener for the click.

Then your_image.src= your_image.src; And the gif will run again.

like image 86
bobbdelsol Avatar answered Oct 06 '22 23:10

bobbdelsol


What 9GAG essentially did was it had two images - one was the animated GIF, and the other was a still JPG.

Before you clicked on it, it showed the JPG file. After you clicked on it, it loaded the GIF into the same <img> tag, and made you think it "played" the GIF image.

If you click on it again, it loads the JPG file back into the <img> tag, and made you think it "paused" the GIF image.

Image manipulation, like only using one GIF and pausing and playing it is far, far too difficult to be of practical use - this method is one of the better ways to do it.

Here's some code:

<div id="gifdiv">
  <img src="staticgif.jpg" />
</div>
<script type="text/javascript">
  $("#gifdiv").click(function () {
    if ($(this).find("img").attr("data-state") == "static") {
      $(this).find("img").attr("src", "animatedgif.gif");
    } else {
      $(this).find("img").attr("src", "staticgif.jpg");
    }
  });
</script>

Also, if you wish for speed, I think 9GAG does it by preloading the GIF beforehand, like this:

<script type="text/javascript">
  (new Image()).src = "animatedgif.gif"; // this would preload the GIF, so future loads will be instantaneous
</script>

Hope this helps.

like image 36
Lucas Avatar answered Oct 07 '22 01:10

Lucas