html - 单个元素上的 CSS 多个动画,没有 javascript

标签 html css animation css-animations

我正在尝试仅使用 CSS 而不使用 Javascript 创建图像幻灯片。

我这里有一个几乎可以工作的例子:http://codepen.io/k/pen/Dkhei . 问题是当我停止将鼠标悬停在其元素上时,“上一个”功能的动画不会暂停。

我在单个元素上定义了两个动画,其类名为“imageContainer”。我很想知道我的语法是否错误,或者我尝试做的事情是不可能的。

HTML:

<div class='carouselContainer'>
<div class='directionSelector previous'>Previous</div>
<div class='directionSelector next'>Next</div>
<div class='imagesContainer'>
  <img class='visible' src='http://ibmsmartercommerce.sourceforge.net/wp-content/uploads/2012/09/Roses_Bunch_Of_Flowers.jpeg'/>
  <img class='hidden' src='http://houseoflowers.com/wp-content/uploads/2013/03/Splendid-flowers-wallpaper-wallpapers-1920x1200-mrwallpaper-com.jpg'/>
  <img class='hidden test' src='http://wallzpoint.com/wp-content/gallery/flower-4/flower-flowers-31723005-1600-1200.jpg'/>
  <img class='hidden' src='http://images2.layoutsparks.com/1/179204/no-idea-t5-flowers.jpg'/>
  <img class='hidden' src='http://3.bp.blogspot.com/-Ca_H0XQYQI4/UQFMSauQxTI/AAAAAAAABik/WHzskd_HqqU/s1600/flowers.jpg'/>
</div>
</div>

CSS:

.carouselContainer{
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  border: 1px solid grey;
  width: 450px;
  height: 300px; 
  position: relative;
  background-color: grey;
  margin: auto;
  overflow: hidden;
}

.directionSelector{
  width: 60px;
  height: 1.5em;
  line-height: 1.5em;
  top: 150px !important;
  position: absolute;
  cursor: pointer;
  background-color: rgba(1,1,1,.7);
  color: white;
  text-align: center;
  border-radius: 5px;
}
.previous{
  top: 0;
  left: 5px;
}
.next{
  top: 0;
  right: 5px;
}
img{
  width: 100%;
  height: 300px;
  float: left;
}
.imagesContainer{
  width: 100%;
  background-color: yellow;
  -webkit-animation-name: showImagesPrev,showImagesNext;
  -webkit-animation-duration: 5s,5s;
  -webkit-animation-play-state: paused,paused;
  -webkit-animation-fill-mode: forwards,forwards;

  -moz-animation-name: showImagesPrev,showImagesNext;
  -moz-animation-duration: 5s,5s;
  -moz-animation-play-state: paused,paused;
  -moz-animation-fill-mode: forwards,backwards;

  animation-name: showImagesPrev,showImagesNext;
  animation-duration: 5s,5s;
  animation-play-state: paused,paused;
  animation-fill-mode: forwards,backwards;

}
.previous:hover ~ div.imagesContainer{
  -webkit-animation-name: showImagesPrev;
  -webkit-animation-play-state: running; 

  -moz-animation-name: showImagesPrev;
  -moz-animation-play-state: running; 

  animation-name: showImagesPrev;
  animation-play-state: running; 
}
.next:hover ~ div.imagesContainer{
  -webkit-animation-name: showImagesNext;
  -webkit-animation-play-state: running;

  -moz-animation-name: showImagesNext;
  -moz-animation-play-state: running;

  animation-name: showImagesNext;
  animation-play-state: running;
}
@-webkit-keyframes showImagesPrev{
  from{
    margin-top: 0px;
  }
  to{
    margin-top: -1200px;
  }
}
@-moz-keyframes showImagesPrev{
  from{
    margin-top: 0px;
  }
  to{
    margin-top: -1200px;
  }
}
@keyframes showImagesPrev{
  from{
    margin-top: 0px;
  }
  to{
    margin-top: -1200px;
  }
}
@-webkit-keyframes showImagesNext{
  from{
    margin-top: -1200px;
  }
  to{
    margin-top: 0px;
  }
}
@-moz-keyframes showImagesNext{
  from{
    margin-top: -1200px;
  }
  to{
    margin-top: 0px;
  }
}
@keyframes showImagesNext{
  from{
    margin-top: -1200px;
  }
  to{
    margin-top: 0px;
  }
}

谢谢你的帮助:)

最佳答案

根据 W3C animation-name 属性 Link

If multiple animations are attempting to modify the same property, then the animation closest to the end of the list of names wins.

在您的示例中,showImagesPrev 首先被引用,showImagesNext 是最后一个,因此它按照 W3C 正常工作。如果您互换这两个引用,Previous 将正常工作,而 Next 按钮会重现该问题。工作Example

关于html - 单个元素上的 CSS 多个动画,没有 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15821245/

相关文章:

html - Microsoft Edge播放曾经是下载的音频文件

jquery - 使用 Jquery 更改 div 宽度

Html5 amp 故事转换为 mo4 视频

css - 溢出 :hidden at a certain height > on hover displays all?

html - 我怎样才能一起翻转和动画图标?

ios - 如何实现iPhone iOS8新建邮件动画

javascript - 当元素未运行时在 C# 中获取 HTML 内部 HTML ="server"

javascript - 未捕获的范围错误 : Invalid string length when appending to a string in JavaScript

html - 我怎样才能只选择第一个不是脚本标签的普通兄弟?

CSS3 Animation Delay 长于 Duration 中断动画