2017-11-25 163 views
0

我只是想通過jquery的animate.css在每次點擊時觸發動畫。我的代碼只是第一次觸發動畫。每次點擊都會觸發動畫jquery

HTML

<input autocomplete="off" type="text" id="a-input" name="input"> 
<button id="next">Next</button> 

JS

$("#next").click(function() { 
      $('#a-input').removeClass().addClass('animated shake'); 
     }); 

我做錯了!

+0

更新的加載相關的CSS代碼 –

+0

相關的CSS是在這裏:[animate.css(https://daneden.github.io/animate.css/) –

回答

0

這是因爲在第二次,按鈕已經有animated shake類。刪除然後添加發生在同一時間...因此,元素沒有區別。

說動畫持續600毫秒......

使用setTimeout()一旦動畫完成,除去這些類。

其結果將是:

$("#next").click(function() { 
    $('#a-input').addClass('animated shake'); 
    setTimeout(function(){ 
    $('#a-input').removeClass(); 
    },650); 
}); 

$("#next").click(function() { 
 
    $('#a-input').addClass('animated shake'); 
 
    setTimeout(function(){ 
 
    $('#a-input').removeClass(); 
 
    },650); 
 
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<input autocomplete="off" type="text" id="a-input" name="input"> 
 
<button id="next">Next</button>

從jQuery的
+0

但不知何故,這不刪除班級,你檢查一下嗎? – Hobo

+0

我添加了一個片段。 –

+0

@LouysPatriceBessette你可以讓我知道如何添加一個片段.. – Danish

1

使用toggleClass

$("#next").click(function() { 
 
     $('#a-input').effect("shake"); 
 
    });
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css"> 
 

 
<script src="//code.jquery.com/jquery-1.12.4.js"></script> 
 
    <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 

 
<input autocomplete="off" type="text" id="a-input" name="input"> <button id="next">Next</button>

AGAIN

+1

你確定嗎?它會工作,如果我使用'$(this)'。 – Hobo

+0

根據要求更新。 @Hobo – Danish

+0

添加了一個片段,只是傾斜如何:) – Danish