【发布时间】:2014-09-07 06:13:37
【问题描述】:
In this fiddle 我有一个 div,它在单击另一个元素时移动,并在单击除 div 本身之外的所有元素时移回。我用布尔值检查它的位置。我遇到的问题是,在布尔值更改后,它无需再次单击即可执行另一个函数,因此 div 只是在每次单击时来回移动。
这是代码:
<p>Click me!</p>
<div></div>
div {
height: 50px;
width: 50px;
background-color: red;
}
$(document).ready(function () {
var isRight = false;
function toRight() {
$('div').animate({'margin-left':'60%'}, 500);
isRight = true;
}
function toLeft() {
$('div').animate({'margin-left':'0'}, 500);
isRight = false;
}
$('*:not(div)').on('click', function() {
if (isRight) {
toLeft();
}
});
$('p').on('click', function () {
if (!(isRight)) {
toRight();
}
});
});
【问题讨论】:
标签: javascript jquery function click boolean