2016-11-03 7 views
2

私は "ボタン"を持っており、クリック後に私は変形スタイルを追加します。 しかし、もう一度クリックすると、このクラスが既に存在するため、このスタイルは機能しません。変換スタイルを追加して、常に動作させる方法を教えてください。

どうすれば変更できますか?取り外しと動作しません単一のブロックにクラスを追加

$('.btn').on('click', function() { 
 
    $(this).addClass('btn_transform'); 
 
});
.btn { 
 
    width: 80px; 
 
    background-color: green; 
 
    height: 20px; 
 
    border: none; 
 
    margin: 50px; 
 
    cursor: pointer; 
 
} 
 

 
.btn_transform { 
 
     transform: rotate(180deg); 
 
     transition: all 0.5s; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<button class="btn"></button>

+1

クラスを削除してみませんか?それは難しいですか? –

答えて

0

。可能な解決策は、クラスを削除して、それは非常に短い時間でsetTimeoutを使用して、後で追加:

$('.btn').on('click', function() { 
 
var self = this; 
 
$(this).removeClass('btn_transform'); 
 
setTimeout(
 
function(){$(self).addClass('btn_transform')} 
 
, 10); 
 
});
.btn { 
 
    width: 80px; 
 
    background-color: green; 
 
    height: 20px; 
 
    border: none; 
 
    margin: 50px; 
 
    cursor: pointer; 
 
} 
 

 
.btn_transform { 
 
     transform: rotate(180deg); 
 
     transition: all 0.5s; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<button class="btn"></button>

+0

なぜトランジェントエンドイベントを使用しないのですか? – Kaiido

1

$(".btn").mouseup(function() { 
 

 
    setTimeout(function() { 
 
    $(".btn").removeClass('btn_transform'); 
 
    }, 300); 
 

 
}); 
 

 

 
$(".btn").mousedown(function() { 
 

 
    $(this).addClass('btn_transform'); 
 

 
});
.btn { 
 
    width: 80px; 
 
    background-color: green; 
 
    height: 20px; 
 
    border: none; 
 
    margin: 50px; 
 
    cursor: pointer; 
 
} 
 
.btn_transform { 
 
    transform: rotate(180deg); 
 
    transition: all 0.5s; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<button class="btn"></button>

$('.btn').on('click', function() { 
 
    $(this).addClass('btn_transform'); 
 
});
.btn { 
 
    width: 80px; 
 
    background-color: green; 
 
    height: 20px; 
 
    border: none; 
 
    margin: 50px; 
 
    cursor: pointer; 
 
} 
 

 
.btn_transform { 
 
     transform: rotate(180deg); 
 
     transition: all 0.5s; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<button class="btn"></button>

enter code here 
+0

はそれぞれmousedown()とmouseup()の2つのイベントを使用します... –

関連する問題