英文:
hover animation one by one in html using css
问题
我有四个div
元素,我正在尝试让它们一个接一个地连续悬停,我的代码如下:
#rij1 .content-overlayz,
#rij1 .content-details {
animation-duration: 2s;
animation-name: stepped-pulse;
animation-iteration-count: infinite;
animation-timing-function: steps(4, end);
}
@keyframes stepped-pulse {
0% {
opacity: 0;
}
25% {
opacity: 0.5;
}
50% {
opacity: 0.8;
}
100% {
opacity: 1;
}
}
我使用以下CSS使第一部分动画:
#rij1 .content-overlayz,
#rij1 .content-details {
animation-duration: 2s;
animation-name: stepped-pulse;
animation-iteration-count: infinite;
animation-timing-function: steps(4, end);
}
如何使剩余的元素像第一个元素一样一个接一个地连续播放,即第一部分会播放,然后停止,然后下一个会播放,然后停止,以此类推,请帮忙,提前感谢。
英文:
I have four divs, which I am trying to hover one after the other continuously, my code is like below:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
#rij1 .content-overlayz,
#rij1 .content-details {
animation-duration: 2s;
animation-name: stepped-pulse;
animation-iteration-count: infinite;
/* animation-direction: alternate; */
animation-timing-function: steps(4, end);
}
@keyframes stepped-pulse {
0% {
opacity: 0;
}
25% {
opacity: 0.5;
}
50% {
opacity: 0.8;
}
100% {
opacity: 1;
}
}
<!-- language: lang-html -->
<a href="<?php echo base_url()?>homecontroller/types/<?=$subz->id?>">
<div class="ProductBlock" id="rij<?=$i?>">
<div class="contentme">
<div class="content-overlayz"></div>
<div class="img-fill">
<img src="<?php echo base_url()?>admin/uploads/types/<?=$subz->pimage?>">
</div>
<div class="content-details" style="z-index:99">
<h3><?=$subz->name?></h3>
</div>
</div>
</div> </a>
<!-- end snippet -->
i used the following css to make the first section animate:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
#rij1 .content-overlayz,
#rij1 .content-details {
animation-duration: 2s;
animation-name: stepped-pulse;
animation-iteration-count: infinite;
/* animation-direction: alternate; */
animation-timing-function: steps(4, end);
}
@keyframes stepped-pulse {
0% {
opacity: 0;
}
25% {
opacity: 0.5;
}
50% {
opacity: 0.8;
}
100% {
opacity: 1;
}
}
<!-- end snippet -->
how do i make rest of the elements animate like this one after the other , that is 1st section will animate then stop then next will animate and stop and next, please help, thanks in advance
答案1
得分: 1
错误: 您正在向动态生成的 ID 添加样式到代码中,但您可能无法在以后将样式添加到所有元素上。
修复方法: 将样式添加到 ProductBlock
类,或者如果您在其他地方使用它,添加到一个新类中。
.animateThis .content-overlayz,
.animateThis .content-details {
animation-duration: 2s;
animation-name: stepped-pulse;
animation-iteration-count: infinite;
animation-timing-function: steps(4, end);
}
@keyframes stepped-pulse {
0% {
opacity: 0;
}
25% {
opacity: 0.5;
}
50% {
opacity: 0.8;
}
100% {
opacity: 1;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="">
<div class="ProductBlock" id="rij1">
<div class="contentme">
<div class="content-overlayz"></div>
<div class="img-fill">
<img src="">
</div>
<div class="content-details" style="z-index:99">
<h3>
Name 1
</h3>
</div>
</div>
</div>
</a>
<a href="">
<div class="ProductBlock" id="rij2">
<div class="contentme">
<div class="content-overlayz"></div>
<div class="img-fill">
<img src="">
</div>
<div class="content-details" style="z-index:99">
<h3>
Name 2
</h3>
</div>
</div>
</div>
</a>
<a href="">
<div class="ProductBlock" id="rij3">
<div class="contentme">
<div class="content-overlayz"></div>
<div class="img-fill">
<img src="">
</div>
<div class="content-details" style="z-index:99">
<h3>
Name 3
</h3>
</div>
</div>
</div>
</a>
<a href="">
<div class="ProductBlock" id="rij4">
<div class="contentme">
<div class="content-overlayz"></div>
<div class="img-fill">
<img src="">
</div>
<div class="content-details" style="z-index:99">
<h3>
Name 4
</h3>
</div>
</div>
</div>
</a>
英文:
Error: You are adding style to id which is being generated dynamically to the code and you might not be able to add the styling to all later on
Fix Add the styling either to ProductBlock
class or to a new class if you are using it elsewhere.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
function playAnimation() {
let currentIndex = 0;
$(".ProductBlock").each(function(index) {
let element = $(this);
setTimeout(function() {
$(".ProductBlock").removeClass("animateThis");
element.addClass("animateThis");
currentIndex++;
if (currentIndex === $(".ProductBlock").length) {
setTimeout(function() {
currentIndex = 0;
playAnimation();
}, 2000);
}
}, index * 2000);
});
}
playAnimation();
});
<!-- language: lang-css -->
.animateThis .content-overlayz,
.animateThis .content-details {
animation-duration: 2s;
animation-name: stepped-pulse;
animation-iteration-count: infinite;
animation-timing-function: steps(4, end);
}
@keyframes stepped-pulse {
0% {
opacity: 0;
}
25% {
opacity: 0.5;
}
50% {
opacity: 0.8;
}
100% {
opacity: 1;
}
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="">
<div class="ProductBlock" id="rij1">
<div class="contentme">
<div class="content-overlayz"></div>
<div class="img-fill">
<img src="">
</div>
<div class="content-details" style="z-index:99">
<h3>
Name 1
</h3>
</div>
</div>
</div>
</a>
<a href="">
<div class="ProductBlock" id="rij2">
<div class="contentme">
<div class="content-overlayz"></div>
<div class="img-fill">
<img src="">
</div>
<div class="content-details" style="z-index:99">
<h3>
Name 2
</h3>
</div>
</div>
</div>
</a>
<a href="">
<div class="ProductBlock" id="rij3">
<div class="contentme">
<div class="content-overlayz"></div>
<div class="img-fill">
<img src="">
</div>
<div class="content-details" style="z-index:99">
<h3>
Name 3
</h3>
</div>
</div>
</div>
</a>
<a href="">
<div class="ProductBlock" id="rij4">
<div class="contentme">
<div class="content-overlayz"></div>
<div class="img-fill">
<img src="">
</div>
<div class="content-details" style="z-index:99">
<h3>
Name 4
</h3>
</div>
</div>
</div>
</a>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论