jQuery Dialog - 动画对话框从中心移动到右上角
我有一个带有验证插件设置的表单。当表单提交并出现错误时,我将在对话框(非模式)中显示错误字段。
表单可能会变得非常长非常快(业务需求不断添加字段),我将对话框中的错误字段设置为可单击,以将窗口滚动到表单中的字段并突出显示这些字段。此时该对话框并未消失。
该对话框一开始就居中,一旦用户开始单击链接,我需要将对话框移出用户的视线并突出显示(jQuery 效果)下面的字段。
为此,我想出了以下代码,并且该对话框似乎动画到顶部而不是右侧。当我将“右”更改为“左”时,效果很好。
下面是代码和要使用的 jsbin - http://jsbin.com/avigi3/4/
$('a.field').click(function(){
$(this).closest('.ui-dialog').animate( {
right : '0', /*left:'0' works fine here*/
top : $(window).scrollTop()
}, 'slow');
});
我调试了 jQuery.animate 代码,以下是我的注释:
- jQuery.animate() 从属性的当前值开始,并逐渐动画到传入的目标值。例如:在调用中,top 以任何当前顶部对话框的值是 并减少到窗口的scrollTop(即浏览器视口的可见顶部)。
- 现在,问题就在这里。即使对话框是居中对齐的,它的“right”属性也不存在,因为 jQuery-ui 使用“left”属性来居中(绝对位置)对话框。由于“右”不存在,“右”的动画序列似乎不起作用。
有人遇到过这个问题吗?有什么方法可以重置对话框的“正确”位置,使其具有“某些”值吗?或者我可以配置 jQuery 对话框,以便在以编程方式更改位置而不是通过手动拖动来更改位置时显示动画。
谢谢,
-赛姆
I have a form with the validation plugin setup. When the form is submitted and has errors, I am showing the error'ed fields in a dialog box (not modal).
The form could get really long really quick (business requirements keep adding fields) and I made the errore'd fields in the dialog box clickable to scroll the window to the field in the from and highlight the fields. The dialog box is not dismissed at this point.
This dialog box is centered to start with and once the user starts clicking on the links, I need to move the dialog box out of the user's way and highlight (jQuery effect) the fields below.
To do this, I've come up with the following code and the dialog seems to animate to the top but not to the right. When I change 'right' to 'left' it works fine.
Below is the code and a jsbin to play with - http://jsbin.com/avigi3/4/
$('a.field').click(function(){
$(this).closest('.ui-dialog').animate( {
right : '0', /*left:'0' works fine here*/
top : $(window).scrollTop()
}, 'slow');
});
I debugged the jQuery.animate code and here are my notes:
- jQuery.animate() 'starts' with the property's current value and gradually animates to the destination value as passed in. For ex: in the call, top starts with whatever the current top value of the dialog is and reduces to the scrollTop of the window (which is the visible top of the browser's viewport).
- Now, here lies the problem. Even though the dialog is center-aligned, its 'right' property doesn't exist as jQuery-ui uses the 'left' property to center (absolutely position) the dialog. And since 'right' doesn't exist, the animation sequence for 'right' doesn't seem to work.
Did anyone face this issue? Any way I possibly could reset the 'right' position of the dialog so that it has 'some' value? Or can I configure jQuery dialog such that it animates when the position is changed programmatically and not manually by dragging.
Thanks,
-Syam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个
Try this
对于未来的读者来说,Shrikant 的答案将是一个更快的修复,并且特定于调用,并且效果很好。只需更改宽度以使用 offsetWidth() 来考虑填充/边距。
我最终完成了 jQuery UI 代码并找到了一个通用的实现。这是针对 jquery-ui 1.8.10 的。
一旦通过计算 {left:xxx, top:xxx} 找出将对话框移动到哪里,jQuery-dialog 就会使用 Position 用于定位和重新定位 ui-dialog div 的实用程序。位置的选项之一是“使用”功能。如果定义了 'using',它会触发该函数并传入 {left:xxx, top:xxx} 的对象。如果没有定义,它只会更新 ui-dialog 上的 css。这是代码片段:
您可以覆盖位置默认值上的“using”属性,但由于我的用法仅适用于对话框,因此我在对话框原型上覆盖了它:
我希望这会有所帮助!
谢谢,
-赛姆
For future readers, Shrikant's answer would be a quicker fix and specific to the call and it works fine. Just change width to use offsetWidth() to consider the paddings/margins.
I ended up following through the jQuery UI code to the end and have found a generic implementation. This is for jquery-ui 1.8.10.
Once it figures out where to move the dialog box by calculating {left:xxx, top:xxx}, jQuery-dialog uses the Position utility to position and re-position the ui-dialog div. One of the options for Position is the 'using' function. If 'using' is defined, it triggers that function and passes in an object of {left:xxx, top:xxx}. If it is not defined, it just updates the css on ui-dialog. Here's a snippet of the code:
You can override the 'using' property on the Position defaults, but since my usage is only for dialogs, I overwrote it on the dialog prototype:
I hope this helps!
Thanks,
-Syam