【发布时间】:2018-11-06 11:43:43
【问题描述】:
我正在建立一个个人网站。我研究并开始使用 CSS 关键帧,但自己找到了使用 jQuery + CSS 关键帧的解决方案。
我现在的问题(我假设有一个简单的答案,因为我一直盯着我的显示器太久了,所以我看不到)是我的悬停动画只能工作一次。也就是说,在鼠标悬停时,我的图像 1 淡入图像 2(图像 1 消失),然后图像 2 淡入图像 3(图像 2 消失),所以它就像一个级联。但是,这只能工作一次,如果我再次悬停,它将无法工作。
所以再次澄清一下,我希望这个动画从 image1 级联淡入淡出,到 image2,到 image3,然后反转出来(image3,到 image2,到 image1,即像一些变形效果)每次 用户鼠标:悬停在原始图像上。
我假设答案可能与添加但未删除的class“动画”有关?我实际上不知道 jQuery,但一直在自学即兴,并认为如果其他人看到我的代码,他们可以发现语法/错误。
也
这是将图像淡化/过渡到另一个的最佳方法吗?我想要一个平滑的淡入淡出,就像一个“变形”——这是我做的最好的方式吗? (jquery+keyframes),我应该添加更多关键帧以获得更平滑的效果吗?
这里是 jsfiddle: http://jsfiddle.net/1xrbxdnk/2/
以及源代码: HTML:
<div class="box">
<img src="http://alpizano.com/assets/images/venom1.png" width="50%" class="top">
<img src="http://alpizano.com/assets/images/venom2.png" width="50%" class="middle">
<img src="http://alpizano.com/assets/images/venom3.png" width="50%" class="bottom">
</div>
JavsScript/jQuery
$("img.top").hover(function() {
$(this).addClass("animated");
$("img.middle").addClass("animated2");
$("img.bottom").addClass("animated3");
})
$("img.bottom").bind("webkitAnimationEnd mozAnimationEnd animationEnd", function() {
$(this).addClass("animated6")
$("img.middle").addClass("animated5")
$("img.top").addClass("animated4")
})
CSS
.box {
position: relative;
}
img {
position: absolute;
}
.middle {
display: none;
}
.bottom {
display: none;
}
@keyframes anim1 {
0% {
opacity: 1;
}
70% {
opacity: 0;
}
}
@keyframes anim2 {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
}
@keyframes anim3 {
0% {
opacity: 0;
}
50% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes anim6 {
0% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 0;
}
}
@keyframes anim5 {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
}
@keyframes anim4 {
0% {
opacity: 0;
}
70% {
opacity: 1;
}
}
.top.animated {
animation: anim1 3s ease-in-out;
opacity: 0;
}
.middle.animated2 {
animation: anim2 3s ease-in-out;
display: block;
opacity: 0;
}
.bottom.animated3 {
animation: anim3 3s ease-in-out;
display: block;
opacity: 1;
}
.bottom.animated6 {
animation: anim6 3s ease-in-out;
opacity: 0;
}
.middle.animated5 {
animation: anim5 3s ease-in-out;
opacity: 0;
}
.top.animated4 {
animation: anim4 3s ease-in-out;
opacity: 1;
}
【问题讨论】:
标签: jquery image animation hover fade