Animated Gif Click Here to Try Again

The Animated GIF is a pop way to visualize a blueprint concept (here's an example of how nosotros did it for the mail text furnishings created with CSS) or show off a brusk video clip. But if you lot have besides many of them in the aforementioned page, it will deviate your user'south focus. For pages that showcase a lot of GIFs, this is bad news.

The solution: serve users with a static image and only allow the animated GIF to run upon user-click. In this brusk tutorial we're going to evidence y'all how to do just that.

  • View Demo
  • Download Source

Getting Started

Brainstorm with preparing the project folders and files which include: an HTML file, the jQuery, and lastly a JavaScript file where nosotros volition write our lawmaking. You may link jQuery to a CDN or grab the copy and link it to your project directory. I would go out the styles and CSS to your imagination. The most essential function is the HTML markup is as follows:

<effigy> 	<img src="img/mobile-wireframe.png" tiptop="300" width="400" alt="Static Image" data-alt="img/mobile-wireframe.gif"> </figure> <!-- add more images -->          

Observe the boosted data-alt aspect in the img element. This is where we store the GIF, in place of the static epitome which we initially serve. You may add more images and also add together a caption for each through the figcaption element.

Afterward that, we will write the JavaScript that will bring the magic. The idea is to serve the GIF paradigm when the user clicks on the image.

The JavaScript

First, we create a function that will call up the GIF paradigm path nosotros have put in the information-alt attribute. We will loop through each of the image and apply the jQuery .data() method to do and so:

var getGif = part() { 	var gif = []; 	$('img').each(office() { 		var information = $(this).information('alt'); 		gif.button(data); 	}); 	return gif; } var gif = getGif();

We run the part and save the output in a variable gif, as above. The gif variable at present contains the path of the GIF from the images in the page.

Image Pre-loading

We at present have a loading problem: with the GIF non nonetheless loaded, there is a take chances that the animated GIF would not play instantly (since the browser would need a few seconds to fully load the GIF). This delay would be felt more significantly when the GIF prototype size is large.

We need to pre-load, or load the GIFs simultaneously as the page is loading.

// Preload all the GIF. var image = [];  $.each(gif, function(index) { 	image[index]     = new Paradigm(); 	image[index].src = gif[alphabetize]; });

Now, open the DevTools then caput over to the Network (or Resources) tab. Yous volition notice that the GIFs are already loaded even though they are saved in the data-alt attribute. And the post-obit is all the code you lot need to do and then.

gif loaded

The last slice of the code is where we bind each figure chemical element that wraps the image with the click event.

The code volition swap the image source betwixt the src attribute where the static image is served and the data-alt attribute where we initially serve the GIF image.

The code will also revert to the static image as the user clicks a second time, "stopping" the animation.

$('figure').on('click', role() {    var $this   = $(this),           $index  = $this.index(),                      $img    = $this.children('img'),           $imgSrc = $img.attr('src'),           $imgAlt = $img.attr('information-alt'),           $imgExt = $imgAlt.carve up('.');              if($imgExt[one] === 'gif') {       $img.attr('src', $img.data('alt')).attr('information-alt', $imgSrc);   } else {       $img.attr('src', $imgAlt).attr('data-alt', $img.information('alt'));   }  });

And that's it. You can smooth the page with styles, for instance, you lot may add a play button overlaying the prototype to indicate that it is "playable" or an animated GIF.

Check out the demo and download the source here.

  • View Demo
  • Download Source

trussellthicand1968.blogspot.com

Source: https://www.hongkiat.com/blog/on-click-animated-gif/

0 Response to "Animated Gif Click Here to Try Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel