MooTools - 提交表单时如何获取鼠标位置?
我在这里想做的是在使用 MooTools 提交表单后显示一个跟随光标的加载框。不过,我已将问题简化为 1 个 div 和 1 个表单。
script:
document.addEvent('domready', function(){
$('test_form').addEvent('submit', function(){
var box = $('box');
document.addEvent('mousemove', function(e){
box.setStyles({
top: e.page.y,
left: e.page.x
});
});
box.setStyle('display', 'block');
return false;
});
});
html:
<div id="box">
</div>
<form id="test_form" action="">
<label>Name: </label><input type="text" name="name" /><br/>
<input type="submit" value="Submit" />
</form>
css:
#box {
width: 50px;
height: 50px;
background-color: blue;
position: absolute;
display: none;
}
#test_form {
margin-left: 150px;
}
提交表单时,会显示隐藏的蓝色 div 并跟随光标。但是,当提交表单时,我无法使 div 出现在鼠标位置。在我们移动鼠标之前,“mousemove”不会触发;因此,蓝色 div 在显示后立即出现在位置 (0,0)。有没有办法在提交表单后立即获取鼠标位置?或者有其他方法可以做到这一点吗?
非常感谢任何建议!
更新:
我不想在提交表单之前添加鼠标事件 (mousemove)。原因很简单,因为我不希望 JavaScript 在不必要时继续检查鼠标位置。只是尽量避免性能问题!
What I'm trying to do here is to show a loading box that follows cursor after submitting a form using MooTools. However, I've simplified the problem into just 1 div and 1 form.
script:
document.addEvent('domready', function(){
$('test_form').addEvent('submit', function(){
var box = $('box');
document.addEvent('mousemove', function(e){
box.setStyles({
top: e.page.y,
left: e.page.x
});
});
box.setStyle('display', 'block');
return false;
});
});
html:
<div id="box">
</div>
<form id="test_form" action="">
<label>Name: </label><input type="text" name="name" /><br/>
<input type="submit" value="Submit" />
</form>
css:
#box {
width: 50px;
height: 50px;
background-color: blue;
position: absolute;
display: none;
}
#test_form {
margin-left: 150px;
}
When the form is submitted, it will show the hidden blue div and it will follow the cursor. However, I can't make the div appear at mouse position when the form is submitted. The 'mousemove' will not fire until we move the mouse; thus, the blue div appears at position (0,0) immediately after showing. Is there a way to get the mouse position right after the form is submitted? Or is there an alternative way to do it?
Any suggestions is greatly appreciated!
Updated:
I don't want to add mouse event (mousemove) before the form is submitted. The reason is simply because I don't want the javascript to keep on checking the mouse position when it's not necessary. Just try to avoid performance issue!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,提交是一个事件,但它的 event.type 是提交并且它不会包含鼠标信息。
你的赌注是重新安排你的 JavaScript,这样它就可以一直安静地移动该框,并在提交时通过更改显示来显示该框。类似的东西:
http://jsfiddle.net/jtLwj/
提交后读取框位置将返回光标:)
缺点:在提交之前更改 invis 框的 css 存在延迟。
编辑更好的版本,无需一直更改 dom:
恐怕这已经是最好的了。对事件对象的引用的占用空间最多是最小的。
小提琴:http://jsfiddle.net/dimitar/jtLwj/1/
basically, the submit is an event but its event.type is submit and it won't contain mouse info.
your bet is to re-arrange your javascript so it moves the box quietly all the time and just shows the box by changing display when submitted. something like that:
http://jsfiddle.net/jtLwj/
reading the box position after submit will return your cursor :)
downside: latency of changing css for the invis box before submit.
edit better version w/o the change to dom all the time:
this is as good as it will get, I'm afraid. the footprint of the reference to the event object is minimal at best.
fiddle: http://jsfiddle.net/dimitar/jtLwj/1/