最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CSS transition property on page exit - Stack Overflow

programmeradmin2浏览0评论

I need a few objects on my pages to animate out when a user clicks a link. I want each object to scale and fade out but not all objects such as the navigation buttons.

I was thinking that upon a user clicking a link, the page delays 1 second before opening the redirecting the link to allow fade out giving the animation time to take effect.

I need a few objects on my pages to animate out when a user clicks a link. I want each object to scale and fade out but not all objects such as the navigation buttons.

I was thinking that upon a user clicking a link, the page delays 1 second before opening the redirecting the link to allow fade out giving the animation time to take effect.

Share Improve this question edited Sep 18, 2013 at 13:55 Alex Shesterov 27.6k14 gold badges89 silver badges108 bronze badges asked Sep 18, 2013 at 13:29 user2761443user2761443 2
  • could you show your attempts in a fiddle? – Fabrizio Calderan Commented Sep 18, 2013 at 13:34
  • Take a look at below web site on Using CSS transitions. developer.mozilla/en-US/docs/Web/Guide/CSS/… – Jocelyn Commented Sep 18, 2013 at 13:44
Add a ment  | 

1 Answer 1

Reset to default 5

Look at the JS event window.onbeforeunload

https://developer.mozilla/en-US/docs/Web/API/window.onbeforeunload

It will hopefully be enough to just run the exit animations when this function is triggered - it generally takes the browser around a second to unload the page pletely but this varies depending on your browser, page size and cpu speed.

Assuming you're using plain JS and you know how to do CSS transitions, the simple way to make animations occur on page exit is something like this:

window.onbeforeunload = function(e){
    document.getElementById('myDiv').className = 'out';
}

Where myDiv id the element you want to animate and out is the CSS class representing the final stage of your transition.

Here is a JSfiddle: http://jsfiddle/X5vKS/

If you need finer control over the wait time, you could use the onbeforeunload function with setTimeout to delay the page exit by the length of time of your animation. This is slightly plex for a JS beginner but is quite doable.

发布评论

评论列表(0)

  1. 暂无评论