[英]Jquery button back to original state
I have a button which if i click rotates to 25deg, I achieved this using Jquery. 我有一个按钮,如果我单击旋转到25deg,则可以使用Jquery实现。 But now I want that if i click it again that it goes to its original state. 但是现在我想要,如果我再次单击它,它将恢复到原始状态。 This is what I got: 这就是我得到的:
$(document).ready(function() {
$(function() {
$('.fa-plus-circle').click(function() {
$(this).prev('p').slideToggle();
$(this).css('transform', 'rotate(20deg)');
$(this).css('transform', 'rotate(-20deg)');
return false;
});
});
});
As you can see i tried adding: 如您所见,我尝试添加:
$(this).css('transform', 'rotate(-20deg)');
right after the first rotation. 第一次旋转后。
You can use a flag to determine whether it is rotated or not. 您可以使用标志来确定它是否旋转。 Here is an example: 这是一个例子:
var rotated = false; $('button').click(function() { $(this).css('transform', rotated ? 'rotate(0deg)' : 'rotate(20deg)'); rotated = !rotated; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>Button</button>
$(document).ready(function() { $(function() { $('.fa-plus-circle').click(function() { $(this).toggleClass('rotated'); return false; }); }); });
.fa-plus-circle { background: blue; height: 50px; width: 50px; margin: 10px; } .fa-plus-circle.rotated { transform: rotate(20deg); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="fa-plus-circle"></div>
Use this:
$(function() {
$('.fa-plus-circle').click(function() {
var el = $(this);
el.prev('p').slideToggle();
if(el.hasClass('active')) {
el.css('transform', 'rotate(0deg)');
} else {
el.css('transform', 'rotate(20deg)');
}
el.toggleClass('active');
return false;
});
});
<button class="fa-plus-circle">
Hello
</button>
$(document).ready(function() {
var rotated = false;
$('.fa-plus-circle').click(function() {
if(rotated === false) {
$(this).css('transform', 'rotate(20deg)');
rotated = true;
} else {
$(this).css('transform', 'rotate(0deg)');
rotated = false;
}
return false;
});
});
Use the following simple approach to check if an element has been transformed: 使用以下简单方法检查元素是否已转换:
$(document).ready(function() { $(function() { $('.fa-plus-circle').click(function() { $(this).prev('p').slideToggle(); var transformed = $(this).css('transform') !== "none"; $(this).css('transform', (transformed)? 'none': 'rotate(-20deg)'); return false; }); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <button class="fa-plus-circle">hello</button>
https://jsfiddle.net/9ppgbsb0/ https://jsfiddle.net/9ppgbsb0/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.