junihh
junihh

Reputation: 502

TranslateX an object and keep the final position

I wrote a test with CSS transform-translateX. The animation works but the object doesn't keep the final position, returns to the original position.

How can keep the final position?

Here the CSS:

@-webkit-keyframes mover {
    0% { -webkit-transform: translateX(0); }
    100% { -webkit-transform: translateX(300px); }
} 
@-moz-keyframes mover {
    0% { -moz-transform: translateX(0); }
    100% { -moz-transform: translateX(300px); }
} 
@-o-keyframes mover {
    0% { -o-transform: translateX(0); }
    100% { -o-transform: translateX(300px); }
} 
@keyframes mover {
    0% { transform: translateX(0); }
    100% { transform: translateX(300px); }
}

#box {
    position: relative;
    top: 20px;
    left: 20px;
    width: 50px;
    height: 50px;
    background-color: #666;
}

.box-mover {
    -webkit-animation: mover 2s ease-in-out;
    -moz-animation: mover 2s ease-in-out;
    -o-animation: mover 2s ease-in-out;
    animation: mover 2s ease-in-out;
}

Here the test: http://jsfiddle.net/junihh/1v0t9vs2/

Thanks for any help with this.

Upvotes: 3

Views: 4584

Answers (1)

Schmalzy
Schmalzy

Reputation: 17324

Add the forwards keyword. DEMO

.box-mover {
    -webkit-animation: mover 2s ease-in-out forwards;
    -moz-animation: mover 2s ease-in-out forwards;
    -o-animation: mover 2s ease-in-out forwards;
    animation: mover 2s ease-in-out forwards;
}

This will set the animation-fill-mode: property.

Forwards - The target will retain the computed values set by the last keyframe encountered during execution. The last keyframe encountered depends on the value of animation-direction and animation-iteration-count:

Upvotes: 5

Related Questions