Changing Background Image with CSS3 Animations

2019-01-03 03:15发布

Why this isn't working? What am I doing wrong?

CSS

@-webkit-keyframes test {
  0% {
    background-image: url('frame-01.png');
  }
  20% {
    background-image: url('frame-02.png');
  }
  40% {
    background-image: url('frame-03.png');
  }
  60% {
    background-image: url('frame-04.png');
  }
  80% {
    background-image: url('frame-05.png');
  }
  100% {
    background-image: url('frame-06.png');
  }
}

div {
  float: left;
  width: 200px;
  height: 200px;
  -webkit-animation-name: test;
  -webkit-animation-duration: 10s;
  -webkit-animation-iteration-count: 2;
  -webkit-animation-direction: alternate;
  -webkit-animation-timing-function: linear;
}

DEMO

http://jsfiddle.net/hAGKv/

Thanks in advance!

11条回答
做自己的国王
2楼-- · 2019-01-03 03:51

Background image isn't a property that can be animated - you can't tween the property.

Instead, try laying out all the images on top of each other using position:absolute, then animate the opacity of all of them to 0 except the one you want repeatedly.

查看更多
我命由我不由天
3楼-- · 2019-01-03 03:51

Works for me. Notice the use of background-image for transition.

#poster-img {
  background-repeat: no-repeat;
  background-position: center;
  position: absolute;
  overflow: hidden;
  -webkit-transition: background-image 1s ease-in-out;
  transition: background-image 1s ease-in-out;
}
查看更多
戒情不戒烟
4楼-- · 2019-01-03 03:53

Like the above stated, you can't change the background images in the animation. I've found the best solution to be to put your images into one sprite sheet, and then animate by changing the background position, but if you're building for mobile, your sprite sheets are limited to less than 1900x1900 px.

查看更多
Deceive 欺骗
5楼-- · 2019-01-03 03:55

You can use animated background-position property and sprite image.

查看更多
小情绪 Triste *
6楼-- · 2019-01-03 03:55

Well I can change them in chrome. Its simple and works fine in Chrome using -webkit css properties.

查看更多
登录 后发表回答