[英]How to move(animate) a button to a certain position on click and return to original position if click elsewhere?
假设我们有一个按钮<button id="btn">I'm a button</button>
它的位置在中央。现在,当我单击此按钮时,它应该移动到左上角。在其他位置单击时在屏幕上,将按钮返回到原始位置(中心),以便下次我单击该按钮时,它应该重复过渡。
您可以使用jQuery.animate()
实现此目的。
Maybe this is the easiest solution for you. 也许这是最简单的解决方案。 Pure css without any javascripts.
没有任何JavaScript的纯CSS。
#btn { position: fixed; right: 50%; top: 50%; margin-right: -84px; transition: all 0.3s linear; } #btn:focus { top:0; right: 100%; }
<button id="btn">I'm a button</button>
You can also use jquery, binding "focus" and "blur" event on the button, and animate it to the #btn:focus style values. 您还可以使用jquery,在按钮上绑定“ focus”和“ blur”事件,并将其设置为#btn:focus样式值的动画。
I tried to make it with javascript maybe like this you mean https://jsfiddle.net/90c23wbr/ 我试图用javascript做到这一点,也许你的意思是https://jsfiddle.net/90c23wbr/
<div class="text-center">
<button id="button" class='btn btn-primary to-center'>button</button>
</div>
css : css:
.btn {
position:absolute;
transition: 1s ease;
}
.to-center {
top:50%;
}
.to-top {
top:5%;
}
and js: 和js:
var removeClass = true;
$("#button").click(function () {
$(".btn").addClass('to-top');
removeClass = false;
});
$("#button").click(function() {
removeClass = false;
});
$("html").click(function () {
if (removeClass) {
$(".btn").removeClass('to-top');
}
removeClass = true;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.