2017-06-01 9 views
0

私は学生ですが、gifをクリックするとgifを開始、一時停止、開始しようとしていますが、このクリック機能を追加する方法は固まっています。私は、オブジェクトのgifバージョンが.images.fixed_height.urlであり、静止画像が.images.fixed_height_still.urlであることを知っています。私が$(この)のように追加しようとすると、そのイメージは未定義になります。私はこれをどうやって行こうか?カテゴリをクリックすると、現在10個のGIFが表示されます。事前に助けていただきありがとうございます。jQueryを使用してgifを一時停止して開始する方法AJAX

コード:あなたはhttp://rubentd.com/gifplayer/

<img class="gifplayer" src="media/banana.png" /> 

<script> 
    $('.gifplayer').gifplayer(); 
</script> 

このjQueryプラグインを使用することができます

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

答えて

1

fixed_heightとfixed_height_stillの違いによって問題は解決します。あなたがよく見ると、URLはname_s.gifとname.gifだけが違っています。

2つの画像を交換してプレーヤーを作成するだけです。これは、演劇のように動作し、停止します。再生や一時停止はしません。しかし、小さなGIFでは、私は休憩が本当に重要だとは思わない、停止と一時停止は同様に見えます。

再生を制御し、これは素晴らしいソリューションです https://jsfiddle.net/karthick6891/L9t0t1r2/

+0

$('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デモを停止します#photo

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

イベントハンドラにクラス名を追加!なぜ$(文書)ではなく$( '本文')を選択するのですか? – willking

+0

私のプロジェクトではそのタイプに慣れています。私たちは身体の出来事を起こします。 :-) – karthick

+0

はあなたの助けに感謝します。@karthick! – willking

0

プログラム的

$('#banana').gifplayer('play'); 
$('#banana').gifplayer('stop'); 
をプレーヤーを再生および停止するには、これらのメソッドを使用し

このように、あなたがコントロールすることができます

詳細はこちらhttps://github.com/rubentd/gifplayer

関連する問題