2017-05-15 3 views
1

그래서 저는 부트 캠프에 있는데, 7 주부터 시작하겠습니다. API 할당이 있었고 Giphy API를 사용하기로 결정했습니다. 좋아, 나는 ajax 호출을했고, json 객체를 가지고 있고, 버튼 클릭으로 gif를 표시하고있다. GIF가로드되면 이미지를 애니메이션으로 만들 수 있어야하며 다음 클릭을하면 이미지가 다시 나타납니다. 프로세스가 스스로를 반복 할 수 있어야합니다. 나는 그것이 의미있는 길을 설정했다. 그러나, 나는 그것이 지금 쓰여지는 방식으로 클릭시 변경 될 URL을 얻는 것처럼 보이지 않는다. 나는 .splice URL을 시도하고 concat을 사용하여 업데이트 된 URL을 완성했다. 그러나, 나는 다음 번 클릭시에 여전히 상태로 되돌아 가도록 이미지를 얻는 방법을 찾지 못합니다 ... 많은 시간 동안 sooooo에 대해이 작업을하고 있었는데 누군가 제게 신선한 눈을 빌려주시겠습니까? 에 다시 한번 감사정지 상태에서 애니메이션 상태로 전환하려고합니다. giphy API를 사용합니다.

<body> 
 
    <div class="jumbotron"> 
 
     <div class="container"> 
 
      <div class="myHeader"> 
 
       <h1>WordSmiths</h1> 
 
       <span class="text-muted" id="jtronText"> 
 
        <div id="sometimeGone">Sometimes</div><div id="loadTimer">someone opens their mouth and you can't help but to be dazzled with the magic.</div> 
 
       </span> 
 
       <p class="text-success" id="instructionsOne"> 
 
        Click a button, see what happens! 
 
       </p> 
 
       <p class="text-success" id="instructionsTwo"> 
 
        Now you can create your own buttons!!! 
 
       </p> 
 
      </div> 
 
     </div> 
 
    </div> 
 
    <div class="container"> 
 
     <div class="row"> 
 
      <div class="col-lg-12"> 
 
       <div class="buttonGallery"></div> 
 
       <h4>Create buttons, find Gifs!: </h4> 
 
       <form id="btnGeneratingForm"> 
 
        <label for="input">Anything: </label> 
 
        <input type="text" id="user-input"> 
 
        <br> 
 
        <input type="submit" id="anotherButton" value="More buttons"> 
 
       </form> 
 
      </div> 
 
     </div> 
 
    </div> 
 
    <div class="row"> 
 
     <div class="col-lg-12"> 
 
      <div id="gif-Gallery"></div> 
 
     </div> 
 
    </div>

:

여기 내 JS와 JQuery와

// $("#loadTimer, .buttonGallery, #btnGeneratingbtn").hide(); 
 
var topics = ["Brandon", "Biggie Smalls", "Dr. Seuss", "Paul McCartney", "John Lennon", "Will Ferrell", "Jimmy Fallon", "Chris Farley", "Dane Cook", "Eminem", "Nas", "Jay-Z", "Rakim", "William Shakespeare","Jim Morrison", "James Maynard", "Serj Tankian"]; 
 
var gifcount = 0; 
 
var gifLocation; 
 
var clickCount = 0; 
 

 
var buttonFactory = function() { 
 
    $(".buttonGallery").empty(); 
 

 
    for (i = 0; i < topics.length; i++) { 
 
     var imAbutton = $("<button>"); 
 
     imAbutton.addClass("yup"); 
 
     imAbutton.attr("data-name", topics[i]); 
 
     imAbutton.text(topics[i]); 
 
     $(".buttonGallery").append(imAbutton); 
 

 
    }}; 
 

 
buttonFactory(); 
 

 

 

 

 

 

 
$("#anotherButton").on("click", function(event) { 
 

 
     event.preventDefault(); 
 
     // This line grabs the input from the textbox 
 
     var onemorebutton = $("#user-input").val().trim(); 
 
     // Adding movie from the textbox to our array 
 
     topics.push(onemorebutton); 
 
     // Calling renderButtons which handles the processing of our movie array 
 
     buttonFactory(); 
 
     }); 
 

 

 
$(".yup").on("click", function(){ 
 
    
 

 
    
 
    $("#gif-Gallery").empty(); 
 
    var searchTermUpdate; 
 
    var searchTerm = $(this).attr("data-name"); 
 
     // removing white space between two-word strings, replacing with a "+" \\ 
 
searchTermUpdate = searchTerm.replace(/ +/g, "+"); 
 

 
    var queryURL = "http://api.giphy.com/v1/gifs/search?q=" + searchTermUpdate + "&api_key=dc6zaTOxFJmzC&limit=10"; 
 
    $.ajax({ 
 
     url: queryURL, 
 
     method: 'GET' 
 
    }).done(function(response) { 
 
     console.log(response); 
 
     
 
     var results = response.data; 
 
    
 
     for (var i = 0; i < results.length; i++){ 
 
      gifcount = gifLocation; 
 
      
 
      
 
      var gifDIV = $("<div class='unit' data-state='still'>"); 
 
      var pRating = $("<p>").text("Rating: " + results[i].rating); 
 
      var gifImg = $("<img id='gifimg' class='col-md-4'>"); 
 
      gifImg.attr("src", results[i].images.fixed_height_still.url); 
 
      gifImg.attr({'data-animate' : results[i].images.fixed_height.url}); 
 
      gifImg.attr({'data-state' : "still"}); 
 
      gifImg.attr({'data-still' : results[i].images.fixed_height_still.url}); 
 
      gifDIV.append(pRating); 
 
      gifDIV.append(gifImg); 
 
      gifDIV.append(gifLocation); 
 
      
 
      gifcount++; 
 

 
      
 

 
      // appending gif div to DOM \\ 
 
      if (results[i].rating !== "g" || "pg" || "pg-13" || "y"){ 
 
      $("#gif-Gallery").append(gifDIV); 
 
     }} 
 

 
     $(".unit").on("click", function(){ 
 

 

 
        var state = $(this).attr('data-state'); 
 
        
 
       
 
       if (state === "still") { 
 
     $(this).attr("src", $(this).attr("data-animate")); 
 
     $(this).attr("data-state", "animate"); 
 
     } else { 
 
     $(this).attr("src", $(this).attr("data-still")); 
 
     $(this).attr("data-state", "still"); 
 
     } 
 
     
 

 
      
 
// var imgPath = this.children[1].src; 
 
      
 
      clickCount++; 
 
      var a; 
 
      var b; 
 
      var c; 
 
      var d; 
 
      var f; 
 
      
 
        
 
        // if (clickCount % 2 === 0){ 
 
        
 
        // for (i=0; i < imgPath.length; i++){ 
 
        //  // locating index of underscore, storing that value in var a\\ 
 
        //  var a = imgPath.indexOf("_"); 
 
        //  // removing all characters to the right of the underscore \\ 
 
        //  var b = imgPath.slice(0, a); 
 
        //  f = b; 
 

 
        //  // setting var c to a string value of .gif \\ 
 
        //  var c = ".gif"; 
 
        //  // combining var b and var c to complete updated image path \\ 
 
        //  var d = b.concat(c); 
 
        //  } 
 
       
 
         // setting image url to animated url \\ 
 
        // $(gifImg).attr("src", d); 
 

 
         
 
        // this.children[1].src = d; 
 
}); 
 
    });});   //

을 그리고 여기에 필요한 경우 HTML입니다 사전에! 로버트

답변

0

나는 실제로 같은 프로젝트를하고 있습니다! 나는 그것을 꽤 비슷한 방식으로했다. 문제는 자식 이미지 대신 이미지 홀더 div에 애니메이션 src를 추가하는 것일 수 있습니다. 이 행은 $ (this) .attr ("src", $ (this) .attr ("data-animate"));

나는 당신에게 문제가 있다고 생각합니다.

프로젝트에 대한 행운을 빈다. 도움이되기를 바랍니다.

+1

네, 감사합니다. Lol. 나는 지난 주 언젠가 그것을 실제로 알아 냈을 것이다. 아마 이것을 업데이트해야만했다. 어느 쪽이든, 고마워! –

+0

아, 완벽하게 듣고 기쁩니다! 행운을 빌어 요. –