javascript - 如何使用 jQuery AJAX 暂停和启动 gif

标签 javascript jquery ajax onclick gif

我是一名学生,我试图在用户单击 gif 时启动、暂停和启动 gif,但是我不知道如何添加此单击功能。我知道该对象的 gif 版本是 .images.fixed_height.url ,静态图像是 .images.fixed_height_still.url 。如果我尝试像下面 $(this) 那样附加,我会发现图像未定义。我该怎么做呢?当前单击类别时会显示 10 个 gif。感谢您提前提供的任何帮助。

代码:

<!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>

最佳答案

fixed_height和fixed_height_still之间的差异将解决这个问题。如果仔细观察,URL 的区别仅在于 name_s.gif 和 name.gif。

所以你可以简单地交换两个图像来创建一个播放器。这将像一场戏剧一样,然后停止。不播放和暂停。但在小 gif 中,我认为暂停并不重要,停止和暂停看起来很相似。

将类名添加到#photo

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

控制播放和停止的事件处理程序

$('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演示 https://jsfiddle.net/karthick6891/L9t0t1r2/

关于javascript - 如何使用 jQuery AJAX 暂停和启动 gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44298501/

相关文章:

javascript - JQuery UI 选项卡 - 透明背景

javascript - 在多次上传中,所有图像都附加到最后一个容器 div

javascript - 多个 API 请求

jquery - 上下文菜单与 jQuery FullCalendar 集成

javascript - 如何使用 Javascript 获取 xml 文件中纯文本的属性值?

php - 如果来自 PHP 的结果/回显以 "abc"开头,请使用 jquery 检查

javascript - 页面打开时菜单打开(下拉)

jquery - 如何设置 jquery datetimepicker mindate 是明天

jquery - 如何使用JQuery "after"选择器

javascript - 读取通过 Ajax 调用发送的 PHP 中的 json 数据