2016-09-02 6 views
1

私はこの回転divを持っていて、.stop()メソッドを使用してボタンをクリックしたときに停止したいのですが、何らかの理由で動作しません。私はあなたがこの問題について私にとって光を放つことができることを願っています。jQuery stop animationメソッド

アニメーションを実行し、Stop animationボタンがアニメーションを停止しないことを示すスニペットです。

$('button').bind('click', function(){ 
 
    $('.player-disc').stop(true, false); 
 
});
#player { 
 
    position: relative; 
 
    margin: 30px auto; 
 
    height: 300px; 
 
    width: 700px; 
 
    background-color: #E25822; 
 
    -moz-border-radius: 200px; 
 
    -webkit-border-radius: 200px; 
 
    border-radius: 200px; 
 
} 
 
#player .player-disc { 
 
    -moz-animation: spin 5s infinite linear; 
 
    -webkit-animation: spin 5s infinite linear; 
 
    animation: spin 5s infinite linear; 
 
    height: 250px; 
 
    width: 250px; 
 
    background-color: black; 
 
    background-size: 250px; 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 25px; 
 
    transform: translateY(-50%); 
 
} 
 

 
#player .player-disc span { 
 
    position: absolute; 
 
    width:30px; 
 
    height: 30px; 
 
    background-color:white; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50%, -50%); 
 
    cursor: pointer; 
 
} 
 

 
@keyframes spin { 
 
    0% { 
 
    transform: translateY(-50%) rotate(0deg); 
 
    } 
 
    100% { 
 
    transform: translateY(-50%) rotate(360deg); 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button type="button">Stop animation</button> 
 
<div id="player"> 
 
    <div class="player-disc"> 
 
    <span></span> 
 
    </div> 
 
</div>
は、私はまた、 .stop(false, false)など

の任意の他の組み合わせを試してみたjsFiddle

+0

は発表された。 jQueryの '.stop()'は、存在するすべてのメソッドではなく、jQueryメソッドによって開始されたアニメーションを停止するように機能します。アニメーションを停止するには、アニメーションを実行するどのような方法を使用した場合でも影響を与える必要があります。あなたの具体的なケースでは、あなたが得ている答えによって実証されているように、様々な方法があります。 – Makyen

+0

あなたのコードをJSFiddleから質問に移しました。デバッグの質問については、質問自体に[mcve] **を含める必要があります**。問題を再現するのに十分なコードがなければ、質問はトピック外であると考えられ、その理由で終了することがあります。別の時間に、JavaScript/HTML/CSSの問題については、[スニペット](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/)を試してみてください。コードを一つにまとめることができれば、人々が問題の内容を見て、テストで答えを出すことがより簡単になります。 – Makyen

答えて

1

...このコードを試してみてください。

(これはまた、アニメーションをリセット)を停止し、元の位置での使用にディスクをリセットするには:

$('.player-disc').css('animation', 'none'); 

がそれを一時停止し、元の位置での使用にディスクをリセットしないために:

$('.player-disc').css('animation-play-state', 'paused'); 

ますその後、使用してアニメーションを再開することができます。

$('.player-disc').css('animation-play-state', 'running'); 

$('#btn1').bind('click', function(){ 
 
    $('.player-disc').css('animation', 'none'); 
 
}); 
 
$('#btn2').bind('click', function(){ 
 
    $('.player-disc').css('animation-play-state', 'paused'); 
 
}); 
 
$('#btn3').bind('click', function(){ 
 
    $('.player-disc').css('animation-play-state', 'running'); 
 
});
#player { 
 
    position: relative; 
 
    margin: 30px auto; 
 
    height: 300px; 
 
    width: 700px; 
 
    background-color: #E25822; 
 
    -moz-border-radius: 200px; 
 
    -webkit-border-radius: 200px; 
 
    border-radius: 200px; 
 
} 
 
#player .player-disc { 
 
    -moz-animation: spin 5s infinite linear; 
 
    -webkit-animation: spin 5s infinite linear; 
 
    animation: spin 5s infinite linear; 
 
    height: 250px; 
 
    width: 250px; 
 
    background-color: black; 
 
    background-size: 250px; 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 25px; 
 
    transform: translateY(-50%); 
 
} 
 

 
#player .player-disc span { 
 
    position: absolute; 
 
    width:30px; 
 
    height: 30px; 
 
    background-color:white; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50%, -50%); 
 
    cursor: pointer; 
 
} 
 

 
@keyframes spin { 
 
    0% { 
 
    transform: translateY(-50%) rotate(0deg); 
 
    } 
 
    100% { 
 
    transform: translateY(-50%) rotate(360deg); 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button id="btn1" type="button">Set animation to none</button> 
 
<button id="btn2" type="button">Pause animation</button> 
 
<button id="btn3" type="button">Play animation</button> 
 
<div id="player"> 
 
    <div class="player-disc"> 
 
    <span></span> 
 
    </div> 
 
</div>
あなたの問題は、あなたはjQueryのは、[ `.stop()`](https://api.jquery.com/stop/)がどんなにアニメーション、*すべての*アニメーションの効果があるんだと思うことである3210

3

これは、CSSアニメーション、ないあなただけ設定する必要があるので、jQueryのアニメーションですcss animationプロパティto "none"

$('.player-disc').css('animation', 'none'); 
1

あなたのアニメーションはCSSで初期化されています。したがって、CSSで停止する必要があります。 $(".player-disc").css("animation-play-state", "paused");

0

は、他の人が指摘したように、それはCSSアニメーションです

$("button").click(function(){ 
$('.player-disc').css('animation', 'none'); 
}); 

Fiddle