如何在离开页面之前对点击的链接进行动画处理?
这是我想要实现的目标:
- 单击 HTML 链接时,我想在离开页面之前对其进行动画处理
- 如果在新窗口/选项卡中打开链接,则不应发生动画
我到底如何实现这一目标?
Here's what I want to achieve:
- When an HTML link is clicked, I want to animate it before leaving the page
- If the link is opened in a new window/tab, the animation should not take place
How on earth do I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 javascript
但如果您的按钮是链接,则必须停止默认操作。
You could use javascript
But you'll have to stop the default action if your button is a link.
您需要捕获单击事件,阻止默认操作,执行动画,然后加载新页面。
如果使用jquery(可作为a jsfiddle):
更新:这是新的 jsfiddle,它解释了评论中提到的情况,去那里获取更新后的代码。诀窍是查找 ctrl 或 command 的按键,并在按下任一键时中止新定义的动画单击处理程序。
注意:窗口需要获得焦点才能检测到按键,在 jsfiddle 中,这意味着框架需要获得焦点才能工作。
You'll need to capture the click event, prevent the default action, do the animation, then load the new page.
If using jquery (Available as a jsfiddle):
Update: Here's the new jsfiddle that accounts for the situation mentioned in the comments, go there for the updated code. The trick is to look for the keypress of ctrl or command, and abort the newly defined animated click handler if either key is pressed.
Note: The window needs focus before it can detect a keypress, in jsfiddle, that means the frame needs focus before it'll work.