在HTML中使用CSS逐个实现悬停动画。

huangapple go评论66阅读模式
英文:

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 -->

&lt;a href=&quot;&lt;?php echo base_url()?&gt;homecontroller/types/&lt;?=$subz-&gt;id?&gt;&quot;&gt;
      &lt;div class=&quot;ProductBlock&quot; id=&quot;rij&lt;?=$i?&gt;&quot;&gt;
        &lt;div class=&quot;contentme&quot;&gt;
        &lt;div class=&quot;content-overlayz&quot;&gt;&lt;/div&gt;
          &lt;div class=&quot;img-fill&quot;&gt;
            &lt;img src=&quot;&lt;?php echo base_url()?&gt;admin/uploads/types/&lt;?=$subz-&gt;pimage?&gt;&quot;&gt;
          &lt;/div&gt;
          &lt;div class=&quot;content-details&quot; style=&quot;z-index:99&quot;&gt;
          &lt;h3&gt;&lt;?=$subz-&gt;name?&gt;&lt;/h3&gt;
      &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt; &lt;/a&gt;

<!-- 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;
$(&quot;.ProductBlock&quot;).each(function(index) {
let element = $(this);
setTimeout(function() {
$(&quot;.ProductBlock&quot;).removeClass(&quot;animateThis&quot;);
element.addClass(&quot;animateThis&quot;);
currentIndex++;
if (currentIndex === $(&quot;.ProductBlock&quot;).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 -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;a href=&quot;&quot;&gt;
&lt;div class=&quot;ProductBlock&quot; id=&quot;rij1&quot;&gt;
&lt;div class=&quot;contentme&quot;&gt;
&lt;div class=&quot;content-overlayz&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;img-fill&quot;&gt;
&lt;img src=&quot;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;content-details&quot; style=&quot;z-index:99&quot;&gt;
&lt;h3&gt;
Name 1
&lt;/h3&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/a&gt;
&lt;a href=&quot;&quot;&gt;
&lt;div class=&quot;ProductBlock&quot; id=&quot;rij2&quot;&gt;
&lt;div class=&quot;contentme&quot;&gt;
&lt;div class=&quot;content-overlayz&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;img-fill&quot;&gt;
&lt;img src=&quot;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;content-details&quot; style=&quot;z-index:99&quot;&gt;
&lt;h3&gt;
Name 2
&lt;/h3&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/a&gt;
&lt;a href=&quot;&quot;&gt;
&lt;div class=&quot;ProductBlock&quot; id=&quot;rij3&quot;&gt;
&lt;div class=&quot;contentme&quot;&gt;
&lt;div class=&quot;content-overlayz&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;img-fill&quot;&gt;
&lt;img src=&quot;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;content-details&quot; style=&quot;z-index:99&quot;&gt;
&lt;h3&gt;
Name 3
&lt;/h3&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/a&gt;
&lt;a href=&quot;&quot;&gt;
&lt;div class=&quot;ProductBlock&quot; id=&quot;rij4&quot;&gt;
&lt;div class=&quot;contentme&quot;&gt;
&lt;div class=&quot;content-overlayz&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;img-fill&quot;&gt;
&lt;img src=&quot;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;content-details&quot; style=&quot;z-index:99&quot;&gt;
&lt;h3&gt;
Name 4
&lt;/h3&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/a&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月15日 19:42:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定