javascript - 如何使用 vanilla JS 为 wordpress 主题中的每篇文章增加动画延迟

标签 javascript css wordpress wordpress-theming css-animations

我有一个 php 循环来显示 wordpress 主题的存档类别页面上的帖子。我还创建了一个动画,在 .2 秒内将帖子从 opacity=o 变为 opacity=1。我想增加每个附加帖子的动画延迟,以便列表中的第一个帖子出现在下一个帖子之前几毫秒。我已经使用 :nth-of-type(n) 完成了此操作,最大发布限制为 15。我知道这是非常低效的,并且想使用(循环?)JavaScript 来处理动画延迟并增加时间。

@keyframes fade-in {
100% {
    opacity: 1;
    transform: translate(0, 0);
     }
}
#section-grid a:nth-of-type(1) {
    animation: fade-in .2s 0s forwards ease-out;
}

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
      <a href="<?php the_permalink(); ?>">
         <div>
            <img src="<?php the_field('feature_image2'); ?>" alt="plant">
               <aside>
                   <?php the_title(); ?>
               </aside>
         </div>
      </a>

      <?php endwhile; else: ?>
          <div class="page-header">
               <h1>Coming Soon</h1>
          </div>
          <p>If there is nothing here that doesn't mean nothing is coming. Stay tuned for updates and new information.</p>
      <?php endif; ?>

对于存档页面上的每个新帖子,我希望动画延迟增加 0.05 秒。然后我就可以从我的 CSS 中删除 :nth-of-type 了。

提前致谢。

最佳答案

你可以这样使用内联css(这里的延迟以毫秒为单位):

#section-grid a:nth-of-type(1) {
    animation: fade-in .2s 0s forwards ease-out;
}

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$last_anim=0;
?>
      <a href="<?php the_permalink(); ?>" style="animation:fade-in .2s .<?php echo $last_anim; ?>ms forwards ease-out;">
         <div>
            <img src="<?php the_field('feature_image2'); ?>" alt="plant">
               <aside>
                   <?php the_title();
                   $last_anim+=500;
                   ?>
               </aside>
         </div>
      </a>

      <?php endwhile; else: ?>
          <div class="page-header">
               <h1>Coming Soon</h1>
          </div>
          <p>If there is nothing here that doesn't mean nothing is coming. Stay tuned for updates and new information.</p>
      <?php endif; ?>

关于javascript - 如何使用 vanilla JS 为 wordpress 主题中的每篇文章增加动画延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53621542/

相关文章:

css - 在 FF 中显示表行 div 以溢出 "rest"

php - WooCommerce 中的结账付款 Hook

javascript - 如何使用node.js和cheerio抓取谷歌图像

java - 单击链接时需要禁用表

添加为另一个对象的属性时,JavaScript 表单序列化不正确

html - 缩放转换在 Firefox 中不起作用

javascript - 发送后 Node 保存错误 "Can' t 设置 header ”

javascript - 使用 jquery 和 CSS 旋转和调整图像大小

html - 页面中出现边距,这在我自己制作的 wordpress 主题中不是必需的

jquery - 使用 CSS 在移动设备上自动滚动