如何使用 jQuery 在元素淡出延迟后隐藏该元素?
$("div#whitebox p").click(function () {
$("div#blackout").fadeTo(550, 0).delay(555).hide();
});
我试图用上面的代码完成的是让一个名为 blackout 的特定 div 淡出,然后当我单击另一个名为 Whitebox 的 div 中的段落标签时关闭。我需要使用 hide 命令将其关闭的原因是,如果我不这样做,遮光 div 虽然淡出,但仍然会覆盖其他链接并使它们无法单击。然而,使用我正在使用的代码,会发生的情况是淡入淡出动画不起作用,并且停电 div 会立即关闭,甚至没有任何延迟。如何改进此代码,以便淡出动画与 hide() 切换命令一样有效?
$("div#whitebox p").click(function () {
$("div#blackout").fadeTo(550, 0).delay(555).hide();
});
What I'm trying to accomplish with the above code is to have a certain div named blackout fade out and then get toggled off when I click on a paragraph tag in another div called whitebox. The reason why I need to toggle it off using the hide command is because if I don't do so, the blackout div, though faded out, still covers other links and makes them unclickable. However, with the code I am using, what happens is the fade animation doesn't work and the blackout div just gets toggled off instantly without even any delay. How do I improve this code so that the fadeout animation works as well as the hide() toggle command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于不透明度为 0,您正在将其完全淡出。
也许可以考虑: .fadeToggle 它将模拟您正在尝试实现的功能。
You're fading it out completely since your opacity is 0.
Perhaps consider : .fadeToggle which will simulate the functionality you are trying to implement.
您可以使用淡出功能。此函数将设置 div 显示:无(因此您无需调用显式隐藏函数。)
用法:
You can use fadeOut function. This function will set the the div display:none (Hence you need not call an explicit hide function.)
Usage:
Fadeout 本身有一个回调函数:
Fadeout has a callback function itself: