Jquery 卸载效果
我想在用户离开页面时使用 jquerylideUp 效果,只是为了使页面关闭时看起来很酷。
我认为我应该使用 onunload 事件,但是如何才能将页面关闭延迟足够长的时间以使效果运行完成。
我想到的选项之一是有效地劫持页面关闭函数,将其存储在某个变量中,然后在运行效果后执行它,但我不知道如何做到这一点。
任何建议或替代想法都非常受欢迎
I would like to use the jquery slideUp effect when the user navigates away from the page just to make the page look cool as it closes.
I assume that I should use the onunload event but how can I delay the page closing long enough for the effect to run to completion.
One of the options that came to mind is effectively hijacking the page closing function, storing it in some variable and then executing it once I had run my effect but I have no idea how I would do that.
Any suggestions or alternative ideas are more than welcome
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您正在寻找的是 onbeforeunload 事件。
只是一个警告...它应该真的快...或者你的访问者可能会讨厌它,无论它看起来有多酷,
以阻止页面在动画完成之前导航离开,这是一个更大的问题...任何动画都将依赖于 setTimeout/setInterval 并且页面不会在离开之前等待这些完成。
what you're looking for is the onbeforeunload event.
just a warning though... it should be really quick... or your visitors are probably going to hate it no matter how cool it looks
as to preventing the page from navigating away before the animation is done, that's a bigger problem... any animation is going to rely on setTimeout/setInterval and the page won't wait for those to complete before leaving.
当用户要求时,除了关闭窗口之外的任何操作都违反了与用户的合同。 浏览器窗口不是你的,而是用户的,无论效果多么酷,它都不可避免地会惹恼大多数用户。
onbeforeunload 事件的功能非常有限。 它必须返回一个字符串,然后使用该字符串提示用户确认是否离开页面。 它不适用于很酷的动画。
Doing anything but closing the window when the users ask to is breaking a contract with the user. The browser window is not yours, it's the users, and no matter how cool the effect, it will inevitably annoy most of your users.
The onbeforeunload event is very restricted in what it can do. It must return a string, which is then used to prompt the user for a confirmation about leaving the page. It won't work for cool animations.
据我所知,阻止用户离开页面的唯一方法是 onbeforeunload 事件,该事件不可取消。 相反,您从该方法返回一个字符串,浏览器通过“是/否”对话框提示用户,生活继续。 276660 有有关此的更多信息。
我认为你在这件事上不会有太多运气。
As far as I know, the only way to stop a user from leaving a page is the onbeforeunload event, which isn't cancelable. Instead, you return a string from that method, the browser prompts the user with a Yes/No dialog, life goes on. 276660 has more info about this.
I don't think you're going to have much luck with this one.
为什么不,当用户想要离开您的网站时而不是制作“酷”效果(即使用户关闭浏览器/选项卡,卸载事件也会被解雇)并以此骚扰简单的用户...阻止他/她再次返回...
...这样做“酷”效果当用户访问您的网站第一次? 作为正常的介绍效果?
我这样做只是一个简单的想法,您可以在这里看到它: http://www.balexandre.com/jmfc< /a>
why not, instead of making a "cool" effect when a user simple want to go away from your website (even if the user closes the browser/tab the unload event will be fired) and annoying the simple user with that ... preventing him/her to return again...
...do that "cool" effect when a user reaches your website for the first time? as a normal intro effect?
I did that as a simple idea, you can see it here: http://www.balexandre.com/jmfc
我100%同意Jonathan Fingland的回答,并添加此内容。
在 IE 中,(我不确定哪些版本支持此功能,我知道 IE6 支持)您可以使用一些 专有元标记在离开页面时实现淡入淡出等。 但是,这在浏览器中受到限制(仅限 IE),因此您将无法跨浏览器使用。
I would agree 100% with Jonathan Fingland's answer, and add this.
In IE, (I'm not sure what versions support this, I know IE6 did) you can use some propriety meta tags to achieve fades etc when leaving the page. However, this is limited in browsers (IE only), so you're stuck for cross browser use.
您可能会发现通过 AJAX 加载新内容可以让您更好地控制效果和转换,并减少尝试以这种方式劫持浏览器操作而给用户带来的烦恼。
我会考虑使用上面提到的滑块形式(例如参见 http://webdesignledger.com/tutorials/13-super-useful-jquery-content-slider-scripts-and-tutorials),
或者只是加载内容窗格以响应用户点击。
You may find loading new content via AJAX would give you better control of effects and transitions, as well as reducing the annoyance factor to the user which can result from trying to hijack the browser actions in such a manner.
I would look at using a form of slider as mentioned above (see for instance http://webdesignledger.com/tutorials/13-super-useful-jquery-content-slider-scripts-and-tutorials),
or simply loading content panes in response to user clicks.
我发现延迟窗口关闭的唯一方法是使用警报。 如果这对于您的情况来说是可接受的妥协,那么它确实会延迟内存中的窗口销毁,并允许您的页面计时器执行(当然,如果用户没有在动画最终完成之前关闭警报弹出窗口)。
我最近使用了这种方法,因此我可以通过 FABridge 调用 Flex 方法(否则该方法会在 Flex 方法调用完成之前被销毁)。 我想听听您对此的评论。
The only way I've found for delaying the window to close, is using an alert. If this is an acceptable compromise for your case, it will really delay the window destruction in memory, and allow your page timers to execute (of course, if user does not close the alert popup earlier than your animations finalize).
I recently used this approach so i could call a flex method through FABridge (which would otherwise be destroyed before the flex method call finishes). I'd like to hear your comments on this.