CSS transition property on page exit

▼魔方 西西 提交于 2020-01-04 09:23:05

问题


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.


回答1:


Look at the JS event window.onbeforeunload

https://developer.mozilla.org/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 completely 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.net/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 complex for a JS beginner but is quite doable.



来源:https://stackoverflow.com/questions/18873574/css-transition-property-on-page-exit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!