PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 17, 2022

[FIXED] How to pause and start gif using jQuery AJAX

 July 17, 2022     ajax, gif, javascript, jquery, onclick     No comments   

Issue

I am a student and I am trying to start, pause and start a gif when a user clicks the gif, however I am stuck on how to add in this click function. I know that the the gif version of the object is .images.fixed_height.url and the still image is .images.fixed_height_still.url . If I try to append like below $(this) I get that images is undefined. How would I go by doing this? Currently 10 gifs show when you click the category. Thank you for any help in advance.

Code:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Giphy</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

  <style>
    body {
      background-image: url('http://www.efoza.com/postpic/2011/04/elegant-blue-wallpaper-designs_154158.jpg');
      width: 100%;
    }
    button {
      padding: 0 2%;
      margin: 0 2%;
    }
    h4 {
      font-size: 165%;
      font-weight: bold;
      color: white;
    }
    .container {
      background-color: rgba(0, 0, 0, 0.2);
      max-width: 1000px;
      width: 100%;
    }
    .btn {
      margin-top: 2%;
      margin-bottom: 2%;
      font-size: 125%;
      font-weight: bold;
    } 
    .guide {
      padding: 3% 0 0 0;
    }
    .tag-row {
      padding: 3% 0 0 0;
    }
    .category-row {
      padding: 3% 0 ;
    }
    #photo {
      padding-bottom: 3%;
    }
  </style>
</head>

<body>

  <div class="container">
    <div class="row text-center guide"><h4>Click a category and see the current top 10 most popular giphy's of that category!</h4></div>
    <div class="row text-center tag-row" id="tags"></div>
    <div class="row text-center category-row">
      <input type="" name="" id="category"><button class="btn btn-secondary" id="addTag">Add Category</button>
    </div>
  </div>

  <div class="container">
    <div id="photo"></div>
  </div>

  <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
  <script type="text/javascript">

    var tags = ["dog", "dolphin", "whale", "cat", "elephant", "otter"];

    // Function for displaying movie data
      function renderButtons() {

        $("#tags").empty();

        for (var i = 0; i < tags.length; i++) {
          $("#tags").append('<button class="tag-buttons btn btn-primary">' + tags[i] + '</button>');
        }      

      } 

    // Add tags function // 

    $(document).on('click', '#addTag', function(event) {

      event.preventDefault();

      var newTag = $("#category").val().trim();
      tags.push(newTag);

      $("#tags").append('<button class="tag-buttons btn btn-primary">' + newTag + '</button>');

    });

    // Tag button function //

    $(document).on('click', '.tag-buttons', function(event) {

      // Keeps page from reloading //
      event.preventDefault();

      var type = this.innerText;
      console.log(this.innerText);
      var queryURL = "http://api.giphy.com/v1/gifs/search?q=" + window.encodeURI(type) + "&limit=10&api_key=dc6zaTOxFJmzC";

      $.ajax({
        url: queryURL,
        method: "GET"
      }).done(function(response) {
        for (var i = 0; i < response.data.length; i++) {
      $("#photo").append('<img src="' + response.data[i].images.fixed_height_still.url + '" class="animate">');
      $('.animate').on('click', function() {
        $(this).remove().append('<img src="' + response.data[i].images.fixed_height.url + '" class="animate">');
        console.log($(this));
      }); 
    }   
      });

      $("#photo").empty();

    });
    renderButtons();
  </script>
</body>
</html>

Solution

The difference between fixed_height and fixed_height_still will solve the problem. if you look closely the urls differ only by name_s.gif and name.gif.

So you can simply swap the two images to create a player. This will act like a play and stop. Not play and pause. But in a small gif I don't think pause really matter, stop and pause will look similar.

adding class name to the #photo

 $("#photo").append('<img class="gif" src="' + response.data[i].images.fixed_height_still.url + '">');

event handler which will control play and stop

$('body').on('click', '.gif', function() {
    var src = $(this).attr("src");
  if($(this).hasClass('playing')){
     //stop
     $(this).attr('src', src.replace(/\.gif/i, "_s.gif"))
     $(this).removeClass('playing');
  } else {
    //play
    $(this).addClass('playing');
    $(this).attr('src', src.replace(/\_s.gif/i, ".gif"))
  }
});

jsfiddle demo https://jsfiddle.net/karthick6891/L9t0t1r2/



Answered By - karthick
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing