CSS3 动画到悬停状态和返回

标签 css css-transitions css-animations

我需要一个最初没有动画的元素,然后在悬停时动画到不同的状态(一次,无循环),悬停结束后它应该动画回到它的原始状态。

基本上就像使用 :hover 样式和 transition 一样。

有没有办法用 CSS3 动画实现这一点?

这是我当前的用例:http://jsfiddle.net/yjD73/11/

悬停时,元素从不透明度:0 淡化到不透明度:1 并返回。 这是我认为过渡不可能实现的。

编辑:根据此处的要求,来自 jsfiddle 的确切代码

一个包含四张图片的 div

<div class="zoombox">
    <img src="http://maps.googleapis.com/maps/api/staticmap?sensor=false&size=300x300&maptype=hybrid&zoom=4&center=51.561998,-1.605100">
    <img src="http://maps.googleapis.com/maps/api/staticmap?sensor=false&size=300x300&maptype=hybrid&zoom=7&center=51.561998,-1.605100">
    <img src="http://maps.googleapis.com/maps/api/staticmap?sensor=false&size=300x300&maptype=hybrid&zoom=12&center=51.561998,-1.605100">
    <img src="http://maps.googleapis.com/maps/api/staticmap?sensor=false&size=300x300&maptype=hybrid&zoom=16&center=51.562606,-1.605100">
</div>

图像相互堆叠,悬停时有简单的 css 动画

.zoombox {
  position: relative;
  margin: 50px;
  float: left;
}

/* initial state */
.zoombox img:not(:first-child) {
  position: absolute;
  top: 0;
  opacity: 0;
}

/* On hover in */
.zoombox:hover img:nth-child(1) {
  -webkit-animation: first-in 400ms 0ms 1 normal ease-in both;
}

.zoombox:hover img:nth-child(2) {
  -webkit-animation: middle-in 1600ms 0ms 1 linear both;
}

.zoombox:hover img:nth-child(3) {
  -webkit-animation: middle-in 1600ms 1200ms 1 linear both;
}

.zoombox:hover img:nth-child(4) {
  -webkit-animation: last-in 400ms 2400ms 1 linear both;
}

@-webkit-keyframes first-in {
  0% {
    -webkit-transform: scale(1);
    opacity: 1;
  }
  100% {
    -webkit-transform: scale(1.5);
    opacity: 0;
  }
}

@-webkit-keyframes middle-in {
  0% {
    -webkit-transform: scale(0.5);
    opacity: 0;
  }
  25%, 75% {
    -webkit-transform: scale(1);
    opacity: 1;
  }
  100% {
    -webkit-transform: scale(1.5);
    opacity: 0;
  }
}

@-webkit-keyframes last-in {
  0% {
    -webkit-transform: scale(0.5);
    opacity: 0;
  }
  100% {
    -webkit-transform: scale(1);
    opacity: 1;
  }
}

最佳答案

Conic,我创建了一个 JSFiddle,它可以使用 css3 动画复制您想要的大部分内容。

Here it is.

使这一切在 CSS 中成为可能的代码是:

@-webkit-keyframes changeImage {
  0%  {background: url("http://maps.googleapis.com/maps/api/staticmap?sensor=false&size=300x300&maptype=hybrid&zoom=4&center=51.561998,-1.605100");}
  33%  {background: url("http://maps.googleapis.com/maps/api/staticmap?sensor=false&size=300x300&maptype=hybrid&zoom=7&center=51.561998,-1.605100");}
  67%  {background: url("http://maps.googleapis.com/maps/api/staticmap?sensor=false&size=300x300&maptype=hybrid&zoom=12&center=51.561998,-1.605100");}
  100% {background: url("http://maps.googleapis.com/maps/api/staticmap?sensor=false&size=300x300&maptype=hybrid&zoom=16&center=51.562606,-1.605100");}
}

现在 jsfiddle 使图像在悬停时通过动画运行并返回到原始图像。如果您需要任何额外的事情发生,请告诉我,顺便说一句,由于缺乏悬停状态的可能性,这在任何触摸设备上都不起作用。

关于CSS3 动画到悬停状态和返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14171401/

相关文章:

html - 导航栏,最后 li 与其他导航栏不一致,我不能使用导航栏的最后大约 50 像素?

Firefox 和 Explorer 的 css 和 Bootstrap 错误

html - 悬停时动画 2 个侧边框

html - 滞后的 CSS 动画

html - CSS:如何做出正确的布局?

jquery - 点击下拉菜单滚动

css - 从 "auto"动画 flexbox

javascript - 如何为背景图像上的文本剪切动画

CSS3 IE 10 页面淡入淡出过渡/动画

css - 使用 css 变换悬停在外部 div 上的底部边框