【问题标题】:Need onclick to toggle between image and gif from giphy api call需要 onclick 从 giphy api 调用在图像和 gif 之间切换
【发布时间】:2018-05-27 17:08:09
【问题描述】:

::更新代码::

我从一个数组中动态生成了按钮。单击按钮时,来自 API 调用的 10 个静态 gif 图像会附加到页面。单击动态生成的静止图像之一时,我需要显示动画 gif。再次单击后,我需要显示静止图像并隐藏动画 gif。

lastClick = [];

var killersGifs = {

killerSearches: ["Freddy Krueger", "Jason Voorhees", "Pennywise", "Ghostface", "American Mary", "Chucky", "Bride of Chucky", "The Candyman", "Cujo", "Hannibal", "Leatherface", "Michael Myers", "Norman Bates", "Pinhead"],

buttonLoop: function() {
    for (var b = 0; b < killersGifs.killerSearches.length - 1; b++) {
        var buttonM = $("<button class='dynGen'>").text(killersGifs.killerSearches[b]).attr("data-index", killersGifs.killerSearches[b]);
        $("#buttons").append(buttonM);
    }
},

divLoop: function(click) {
    var queryURL = "https://api.giphy.com/v1/gifs/search?api_key=B26sstJns2pZuNT5HiJpqS5FV8Su1sDd&q=" + lastClick + "&limit=10"

  $.ajax({
    url: queryURL,
    method: "GET"
  }).done(function(response) {
    console.log(response.data);

    for (var i = 0; i < response.data.length; i++) {
        var respData = response.data[i];
        var image = respData.images.fixed_height_small_still.url;
        var gif = respData.images.fixed_height_small.url;
        var rating = respData.rating;

        var dynDiv = $("<div class='dyn-div'>");
        //dynDiv.attr("data-index", i);

        var killerImg = $("<img class='still-image'>");

        killerImg.attr("src", image);
        killerImg.attr("alt", "Serial Killer still frame of gif");
        killerImg.attr("data-gif", gif);
        killerImg.attr("class", "killerImg");
        killerImg.attr("data-index", i);


        dynDiv.append("<p> Rating: " + rating + "</p>");
        dynDiv.append(killerImg);

        $("#append-img-div").prepend($(dynDiv));
        };

    });
  },
   userPush: function () {
        var userInput = $("input[type='text']").val().trim();
        console.log(userInput);
        killersGifs.killerSearches.push(userInput);
        var buttonU = $("<button class='dynGen'>").text(userInput).attr("data-index", userInput);
        $("#buttons").append(buttonU);
        console.log(killersGifs.killerSearches);
  }

};

killersGifs.buttonLoop();

$("#killer-add-submit").on("click", function(event) {
    event.preventDefault();
    killersGifs.userPush();
});

$(document).on("click", "button.dynGen", function(event) {
    var currentIndex = $(this).attr("data-index");
    lastClick.push(currentIndex);
    console.log(currentIndex);
    event.preventDefault();
    $("#append-img-div").empty();
    killersGifs.divLoop();
    lastClick = [];
});

$(document).on("click", ".killerImg", function(event) {
    console.log("test");
    var currentIn = $(this).attr("data-index");
    var tempUrl = $(this).attr("data-gif");
    console.log(currentIn);
    console.log(tempUrl);
});

单击时,单击的图像应在静止图像和动画 gif 之间切换。

单击功能控制台记录索引并更正单击图像的 gif URL。我不知道如何合并它以在点击时交换 gif 和图像。

【问题讨论】:

    标签: javascript jquery api onclick giphy-api


    【解决方案1】:

    我认为您需要将 gif url 设置为您使用 jQuery 创建的 img 元素的属性。比如:

    `killerImg.attr("data-gif", gif);`
    

    正如您已经定义的那样 var gif = respData.images.fixed_height_small.url;。您可能还想给它一个唯一的 id,例如: killerImg.attr("id", "killer-img");

    然后,在您的点击事件中,您可以从元素本身检索该属性:

    var tempUrl = $("#killer-img").attr("data-gif");

    并使用 img src 将其切换出来:

    $("#killer-img").attr("data-gif") = $("#killer-img").attr("src");

    最后:

    $("#killer-img").attr("src") = tempUrl设置图片源为动态gif。

    我最近自己处理了一个非常相似的任务。希望这会有所帮助!

    【讨论】:

    • 谢谢!我会在我到我的办公桌前试一试,然后告诉你!
    • 您建议的代码返回了一个错误,提示“分配中的左侧无效”。我编辑了代码以在打印时为每个图像分配一个数据索引号。我不确定如何将其合并到获取正确的 URL 中。现在,它正在返回左上角 gif (data-index-9) 的 URL。打印当前数据索引的控制台日志测试也总是返回 9。
    • ::UPDATED RESULT:: 单击函数控制台日志索引并更正单击图像的 gif URL。我不知道如何合并它以在点击时交换 gif 和图像。
    • 回顾我的类似代码,我注意到对于最后一段,你有你的“如果,否则如果”语句,我有类似的东西,但有 3 个等号,意思是“深度等于” .可能值得尝试,它看起来像:if ($(this).attr("src") === tempUrl2) { //code}。此外,我注意到我在 else 代码块中的 if 和 else if 之后添加了一个 console.log,如果我点击的东西都失败了,它会提醒我。
    • 谢谢杰森!我能够让它与下面的代码一起工作。 ??
    【解决方案2】:
    lastClick = [];
    
    var killersGifs = {
    
    killerSearches: ["Freddy Krueger", "Jason Voorhees", "Pennywise", "Ghostface", "American Mary", "Chucky", "Bride of Chucky", "The Candyman", "Cujo", "Hannibal", "Leatherface", "Michael Myers", "Norman Bates", "Pinhead"],
    
    buttonLoop: function() {
        for (var b = 0; b < killersGifs.killerSearches.length - 1; b++) {
          var buttonM = $("<button class='dynGen'>").text(killersGifs.killerSearches[b]).attr("data-index", killersGifs.killerSearches[b]);
          $("#buttons").append(buttonM);
        }
    },
    
    divLoop: function(click) {
    
        var queryURL = "https://api.giphy.com/v1/gifs/search?api_key=B26sstJns2pZuNT5HiJpqS5FV8Su1sDd&q=" + lastClick + "&limit=10"
    
      $.ajax({
        url: queryURL,
        method: "GET"
      }).done(function(response) {
        console.log(response.data);
    
        for (var i = 0; i < response.data.length; i++) {
            var respData = response.data[i];
            var image = respData.images.fixed_height_still.url;
            var gif = respData.images.fixed_height.url;
            var rating = respData.rating;
    
            var dynDiv = $("<div class='dyn-div'>");
            //dynDiv.attr("data-index", i);
    
            var killerImg = $("<img class='still-image'>");
    
            killerImg.attr("src", image);
            killerImg.attr("alt", "Serial Killer still frame of gif");
            killerImg.attr("data-gif", gif);
            killerImg.attr("class", "killerImg");
            killerImg.attr("data-index", i);
            killerImg.attr("data-img", image);
    
    
    
            dynDiv.append("<p> Rating: " + rating + "</p>");
            dynDiv.append(killerImg);
    
    
            $("#append-img-div").prepend($(dynDiv));
            };
    
        });
    
    
      },
        userPush: function () {
            var userInput = $("input[type='text']").val().trim();
            killersGifs.killerSearches.push(userInput);
            var buttonU = $("<button class='dynGen'>").text(userInput).attr("data-index", userInput);
            $("#buttons").append(buttonU);
            console.log(killersGifs.killerSearches);
      }
    };
    
    killersGifs.buttonLoop();
    
    $("#killer-add-submit").on("click", function(event) {
        event.preventDefault();
        killersGifs.userPush();
    });
    
    $(document).on("click", "button.dynGen", function(event) {
        var currentIndex = $(this).attr("data-index");
        lastClick.push(currentIndex);
        console.log(currentIndex);
        event.preventDefault();
        $("#append-img-div").empty();
        killersGifs.divLoop();
        lastClick = [];
    });
    
    $(document).on("click", ".killerImg", function(event) {
        console.log("test");
        //killersGifs.animateGif();
        var currentIn = $(this).attr("data-index");
        var tempUrl = $(this).attr("data-gif");
        var tempUrl2 = $(this).attr("data-img");
        console.log(currentIn);
        console.log(tempUrl);
        if ($(this).attr("src") == tempUrl2) {
            $(this).attr("src", tempUrl);
        }
        else if ($(this).attr("src") == tempUrl) {
            $(this).attr("src", tempUrl2);
        };
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-24
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      相关资源
      最近更新 更多