How do I change a background position with css3 animation?
Asked Answered
F

2

6

Lets say I have a div element, with a background in position: 0%; how would I change the position to e.g position: 100%; but with keyframes on hover

I can't seem to use keyframes properly, it never works and I have all the latest browsers.

Thanks.

Foundling answered 1/9, 2011 at 21:14 Comment(1)
may I suggest you post some sample code from your keyframe attempts and people can helpTolerant
A
11

If you just want to animate background position on hover it's a lot easier to use a transition instead of keyframe animations. See this fiddle for an example: http://jsfiddle.net/hfXSs/

If you want to put in the extra effort of making it an animation you'll have to set the animation-play-state on the div to 'paused' and change it to 'running' on hover. See the spec on pausing animations here: http://dev.w3.org/csswg/css3-animations/#the-animation-play-state-property-

EDIT: I was bored so here's the same thing using keyframe animations: http://jsfiddle.net/wGRg5/

Obviously, the fiddle has the problem that when you aren't hovering over the div the animation pauses which is probably not the desired effect.

Algol answered 2/9, 2011 at 13:31 Comment(0)
M
6

Some Code, looks like webkit only at this point in time.

.box {
    display:block;
    height:300px;
    width:300px;
    background:url('http://lorempixum.com/300/300') no-repeat;
    background-position:-300px;
    -webkit-transition:background-position 1s ease;
}

.box:hover{
    background-position:0px;
}

Via: http://jsfiddle.net/hfXSs/

More here: http://css-tricks.com/parallax-background-css3/

Media answered 4/6, 2012 at 23:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.