如何使用 css3 动画更改背景位置?
假设我有一个 div 元素,其背景为 position: 0%;
我如何将位置更改为 position: 100%;
但在悬停时使用关键
帧似乎无法正确使用关键帧,它永远无法工作,而且我拥有所有最新的浏览器。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想在悬停时对背景位置进行动画处理,那么使用过渡而不是关键帧动画要容易得多。请参阅此小提琴的示例: http://jsfiddle.net/hfXSs/
如果您想放入要使其成为动画,您必须将 div 上的动画播放状态设置为“暂停”,并在悬停时将其更改为“运行”。请参阅此处有关暂停动画的规范: http:// /dev.w3.org/csswg/css3-animations/#the-animation-play-state-property-
编辑:我很无聊,所以这里使用关键帧做同样的事情动画: http://jsfiddle.net/wGRg5/
显然,小提琴有一个问题,当你不如果将鼠标悬停在 div 上,动画就会暂停,这可能不是所需的效果。
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.
有些代码目前看起来像 webkit。
通过:http://jsfiddle.net/hfXSs/
更多信息:http://css-tricks.com/parallax-background-css3/
Some Code, looks like webkit only at this point in time.
Via: http://jsfiddle.net/hfXSs/
More here: http://css-tricks.com/parallax-background-css3/