我正在构建一个远离鼠标并“围绕”窗口的按钮。我比较新的jQuery/JavaScript,将其作为学习练习(我受此页面#4的启发:http://panjiva.com/jobs/challenges)。if和elseif语句触发
背景:这实际上是我的第二次尝试,可能不是最优雅的解决方案。基本上,在第二个版本中,有隐藏的“临时”按钮与按钮一起移动,并在按钮开始离开屏幕时出现。 (我第一次尝试只有两个按钮,但我遇到了问题)
下面是一个嵌套的if/else语句,用于检测按钮的一部分何时移出窗口,如果是,则显示一个温度按钮在正确的位置给予“包裹”效果。它还检查整个按钮是否移出屏幕,如果是,则将按钮移动到新的坐标处(温度按钮已经暴露)。
我认为这整个if/else语句应该只允许按钮包装到一边,即使按钮留在一个角落。但是,当离开角落时,有时会缠绕到2或3个角落。这是为什么发生?
我创建了一个与的jsfiddle的完整代码:http://jsfiddle.net/aaronmacy/AVwpU/
所有帮助/建议将不胜感激!
// Is any part of the button about to move off the page?
// To the top?
if (edgeTop < 0) {
// Always wrap to the bottom
bottom.show();
// Is the button disappearing?
if (edgeBottom < 0) {
// Move the button
buttonNextY += parseInt(viewportHeight);
moveAll();
// Hide everything else
hideTemps();
}
else {
moveAll();
}
}
// To the right?
else if (edgeRight > parseInt(viewportWidth)) {
// Wrap to the left
left.show();
// Is the button disappearing?
if (edgeLeft > parseInt(viewportWidth)) {
buttonNextX -= parseInt(viewportWidth);
moveAll();
hideTemps();
}
else {
moveAll();
}
}
// To the bottom?
else if (edgeBottom > parseInt(viewportHeight)) {
// Wrap to the top
top.show();
// Is the button disappearing?
if (edgeTop > parseInt(viewportHeight)) {
buttonNextY -= parseInt(viewportHeight);
moveAll();
hideTemps();
}
else {
moveAll();
}
}
// To the left?
else if (edgeLeft < 0) {
// Wrap to the right
right.show();
// Is the button disappearing?
if (edgeRight < 0) {
buttonNextX += parseInt(viewportWidth);
moveAll();
hideTemps();
}
else {
moveAll();
}
}
// If the button is completely inside the window
else {
moveAll();
}
有时您会颠倒moveAll和hideTemps的顺序。这是故意的吗? – 2012-08-17 00:18:02
@SteveWellens不好 - 好。我让它们保持一致,但仍然看到了超过1个包装。 – amacy 2012-08-17 00:25:52
我认为问题出在你在if/else语句中调用hideTemp()的地方。将它从现在的位置删除,并在每个if/else语句后立即放置。它看起来有时2 else语句执行的原因是因为hideTemp被封入在第二个if/else中,而不是每次被调用来清除按钮(不知道!) – 2012-08-17 00:51:58