, top/left margin-top/margin-left , -. , . div, , .
, absolute fixed flex justify-content align-items. divs, , . animation-fill-mode forwards, .
DEMO: http://jsbin.com/secag/1/
HTML:
<div id="container">
<div id="parent">
<div id="div" class="animated running"</div>
</div>
</div>
CSS:
#container {
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
}
#parent {
height: 100%;
display: -webkit-flex;
display: flex;
}
.center-children {
-webkit-justify-content: center;
justify-content: center;
-webkit-align-items: center;
align-items: center;
}
#div {
width: 100px;
height: 100px;
background-color: blue;
}
.animated {
-webkit-animation: moveX 10s linear 0s infinite alternate both, moveY 7s linear 0s infinite alternate;
-webkit-animation-fill-mode: forwards
}
@-webkit-keyframes moveX {
from { left: 0; } to { left: 100%; }
}
@-webkit-keyframes moveY {
from { top: 0; } to { top: 100%; }
}
.paused {
-webkit-animation-play-state: paused;
}
.running {
-webkit-animation-play-state: running;
position: absolute;
}
JS:
document.addEventListener('DOMContentLoaded', function(){
var div = document.getElementById('div');
div.addEventListener('click', function(){
if(this.classList.contains('paused')) {
this.parentElement.classList.remove('center-children');
this.classList.remove('paused');
this.classList.add('running');
} else {
this.classList.remove('running');
this.classList.add('paused');
this.parentElement.classList.add('center-children');
}
});
});
DOCS:
https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content
https://developer.mozilla.org/en-US/docs/Web/CSS/align-items
https://developer.mozilla.org/en-US/docs/Web/CSS/animation-fill-mode