SimpleModal,如何用动画关闭弹出窗口
我对 jQuery 很陌生。我有一个关于 SimpleModal 的问题。
我试图关闭带有动画效果的弹出窗口,但失败了。
这是我的代码。
$('#btnClose').click(function(e) {
// Closing animations
$("#content").modal({ onClose: function(dialog) {
dialog.data.fadeOut('slow', function() {
dialog.container.hide('slow', function() {
dialog.overlay.slideUp('slow', function() {
$.modal.close();
});
});
});
}
});
});
<div id="content" style="display: none;">
<h1>Basic Modal Dialog</h1>
<a href='#' id="btnCloset">Close</a>
</div>
当我单击“关闭”链接时,没有任何反应。有什么帮助吗?非常感谢!
I am very new to jQuery. I have a questino about the SimpleModal.
I am trying to close the pop up window with animation effect, but failed.
Here is my code.
$('#btnClose').click(function(e) {
// Closing animations
$("#content").modal({ onClose: function(dialog) {
dialog.data.fadeOut('slow', function() {
dialog.container.hide('slow', function() {
dialog.overlay.slideUp('slow', function() {
$.modal.close();
});
});
});
}
});
});
<div id="content" style="display: none;">
<h1>Basic Modal Dialog</h1>
<a href='#' id="btnCloset">Close</a>
</div>
When I click on the "Close" link, nothing happens. Any help please? Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原始的、已接受的答案
没有发生任何事情,因为您在 HTML id 标记中拼错了
btnClose
(您将其称为btnCloset
)。尽管如此,它不适用于提供的代码,因为它的作用是这样的:当您单击btnClose
链接时,您将使用#content
,并告诉它当它关闭时,它应该执行onClose
选项中的操作(这是正确的)。所以你实际上并没有告诉它关闭任何地方,只是告诉它这是一个模式对话框。相反,您应该像现在一样从
#content
元素创建模式,但与#btnClose
的点击事件分开进行。然后你应该绑定点击事件来关闭对话框。这是一些代码
As a side note, if you add the class
simplemodal-close
to#btnClose
, simpleModal will automatically make it close the dialog, and you don't have to bind the click event yourself.New answer based on feedback
好吧,我误解了这个插件的工作原理,我认为它就像普通的 jQuery 对话框插件,但据我现在了解,目标是使现有的可见元素成为对话框,并在关闭它时将其转换回原始形式。新代码跟踪对话框的状态(通过将
doOpen
存储在链接的data
中并在每次单击时切换它),并打开和关闭对话框。希望这更接近您想要的工作方式:)Original, accepted answer
Nothing is happening because you misspelled
btnClose
in your HTML id tag (you're calling itbtnCloset
). Nonetheless, it wouldn't work with the provided code, as this is what it does: When you click on thebtnClose
link, you are creating a simpleModal box out of#content
, and telling it that when it closes, it should do the stuff in theonClose
option (which is correct). So you aren't actually telling it to close anywhere, just telling it that it's a modal dialog.Instead you should create the modal out of the
#content
element, as you do now, but do it separately from#btnClose
's click event. Then you should bind click event to close the dialog.Here's some code
As a side note, if you add the class
simplemodal-close
to#btnClose
, simpleModal will automatically make it close the dialog, and you don't have to bind the click event yourself.New answer based on feedback
Ok, i misunderstood how this addon works, i thought it was like the plain jQuery dialog plugin, but as i understand now, the goal is to make an existing, visible element a dialog and when closing it, transform it back to its original form. The new code keeps track of the state of the dialog (by storing
doOpen
in thedata
of the link and toggling it on each click) and both opens and closes the dialog. Hope this is closer to how you wanted it to work :)这是代码,它运行得很好。
Here is the code, which is working perfectly.