while(鼠标在某个边界框中)做某事; -- 在 JavaScript 中
我很困惑。我想做的是:每当我的鼠标指针进入一个盒子时,我想不断改变盒子的颜色。但是,当鼠标离开盒子时,我希望盒子的颜色停止变化。我必须承认我正在学习 JS,变量的作用域给我带来了困难。
在这里:
var t = true;
Crafty.addEvent(this,Crafty.stage.elem,"mousemove",function(e){
if(e.clientX<294)
{
console.log("Left edge");
while(t==true){do something}
}
else if(e.clientY<10)
{
console.log("Top Edge");
}
else if(Math.abs(e.clientX-1084)<10)
{
console.log("Right Edge");
}
else if(Math.abs(e.clientY-600)<10)
{
// console.log("Bottom Edge");
}
else
{
t = false;
}
});
更清楚地说,我想在鼠标位于盒子外面时执行操作(我希望两种情况是等效的:盒子外面仍然是盒子)。上面的代码进入无限循环。
I am stumped. What I want to do is: whenever my mouse pointer enters a box, I want to keep changing the color of the box. However, when mouse leaves the box I want the color of the box to stop changing. I must admit that I am learning JS and scope of variables is giving me hard time.
Here you go:
var t = true;
Crafty.addEvent(this,Crafty.stage.elem,"mousemove",function(e){
if(e.clientX<294)
{
console.log("Left edge");
while(t==true){do something}
}
else if(e.clientY<10)
{
console.log("Top Edge");
}
else if(Math.abs(e.clientX-1084)<10)
{
console.log("Right Edge");
}
else if(Math.abs(e.clientY-600)<10)
{
// console.log("Bottom Edge");
}
else
{
t = false;
}
});
To be more clear, I want to perform an operation when mouse is outside a box(I hope both cases are equivalent: out side a box is still a box). Above code goes into infinite loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样? http://jsfiddle.net/2eWkN/
Like this? http://jsfiddle.net/2eWkN/