淡入淡出幻灯片单张图片

Fade slideshow single image

我的主页上有一个淡入淡出的幻灯片,循环播放 4 张图片。在内页上,我只想展示一张图片,但我希望它执行初始动画然后停止。我正在使用的特定幻灯片使用 CSS 关键帧,但如果我删除 CSS 关键帧或动画 CSS,则页面只是坐在那里,加载时什么也不做。动画 CSS 设置为无限,我知道这是它的一部分,因为它无限循环,但我想做的是在内页上保持相同的一致性,只有一个图像需要初始动画,但在初始动画和图像加载后,幻灯片应该停止。现在,它似乎遍历了所有内容,因此使用 CSS 关键帧加载我的图像,然后它会空白一段时间,最终会再次显示图像。我知道这就是它设计的工作方式,我只是不知道如何调整它以仅处理单个图像而不像它那样循环通过那些 CSS 关键帧。即使 CSS 完全不同,也没关系,因为它将位于完全不同的页面上:-)

HTML:

<div class="slide">
    <div style="background-image:url(https://images.unsplash.com/photo-1575932150875-d31ebc835f67?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1650&q=80)"></div>
    <div style="background-image:url(https://images.unsplash.com/photo-1608500567505-269fa4192c58?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1704&q=80)"></div>
    <div style="background-image:url(https://images.unsplash.com/photo-1477936821694-ec4233a9a1a0?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1620&q=80)"></div>
    <div style="background-image:url(https://images.unsplash.com/photo-1485735662814-c4f66e49dbae?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1650&q=80)"></div>
</div>

CSS:

.slide {
    background: #fff;
    height: 350px;
    overflow: hidden;
    position: relative;
    width: 100%;
}

.slide > div {
    animation: slide 16s infinite;
    background-position: center center;
    background-size: cover;
    height: 100%;
    opacity: 0;
    position: absolute;
    width: 100%;
}

.slide > div:nth-child(2) {
    animation-delay: 4s;
}

.slide > div:nth-child(3) {
    animation-delay: 8s;
}

.slide > div:nth-child(4) {
    animation-delay: 12s;
}

@keyframes slide {
    10% {
        opacity: 1;
    }
    20% {
        opacity: 1;
    }
    30% {
        opacity: 0;
    }
    40% {
        transform: scale(1.2);
    }
}

这是 fiddle: https://jsfiddle.net/nd81hzuc/ 有四张图片:-)

感谢任何帮助!

谢谢,
乔什

您可以使用此 属性 animation-iteration-count: 1; 来决定您的动画 运行 的次数。您可以在 <div class="slide"> 中使用单个图像,这样只有 1 个图像在 @keyframes 中循环,这样可以简化动画:

@keyframes slide {
    10% {
        opacity: 1;
    }
    20% {
        opacity: 1;
    }
    30% {
        opacity: 0;
    }
    40% {
        transform: scale(1.2);
    }
    60% {
        transform: scale(1);
    }
    100% {
        transform: opacity(1);
    }
}

喜欢下面片段中的演示

<!DOCTYPE html>
<html>

<head>
  <style>
    div {
      width: 100px;
      height: 100px;
      background-color: red;
      position: relative;
      animation-name: example;
      animation-duration: 4s;
      animation-iteration-count: 1;
      /*Shorthand property of animation ~ animation: example 4s 1;*/
    }
    
    @keyframes example {
      10% {
        opacity: 1;
      }
      20% {
        opacity: 1;
      }
      30% {
        opacity: 0;
      }
      40% {
        transform: scale(1.2);
      }
      60% {
        transform: scale(1);
      }
      100% {
        transform: opacity(1);
      }
    }
  </style>
</head>

<body>

  <div></div>

</body>

</html>

您可以为单个 img 提供基本相同的关键帧,稍作调整 - 使其保持不透明度:1 并将动画填充模式设置为转发。

<style>
/* Reset */
  
  html,
  body,
  div,
  span,
  applet,
  object,
  iframe,
  h1,
  h2,
  h3,
  h4,
  h5,
  h6,
  p,
  blockquote,
  pre,
  a,
  abbr,
  acronym,
  address,
  big,
  cite,
  code,
  del,
  dfn,
  em,
  font,
  img,
  ins,
  kbd,
  q,
  s,
  samp,
  small,
  strike,
  strong,
  sub,
  sup,
  tt,
  var,
  dl,
  dt,
  dd,
  ol,
  ul,
  li,
  fieldset,
  form,
  label,
  legend,
  table,
  caption,
  tbody,
  tfoot,
  thead,
  tr,
  th,
  td {
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font-weight: inherit;
    font-style: inherit;
    font-size: 100%;
    font-family: inherit;
    vertical-align: baseline;
  }
  /* remember to define focus styles! */
  
  :focus {
    outline: 0;
  }
  
  body {
    line-height: 1;
    color: black;
    background: white;
  }
  
  ol,
  ul {
    list-style: none;
  }
  /* tables still need 'cellspacing="0"' in the markup */
  
  table {
    border-collapse: separate;
    border-spacing: 0;
  }
  
  caption,
  th,
  td {
    text-align: left;
    font-weight: normal;
  }
  
  blockquote:before,
  blockquote:after,
  q:before,
  q:after {
    content: "";
  }
  
  blockquote,
  q {
    quotes: "" "";
  }
  /* Slides */
  
  .slide {
    background: #fff;
    height: 350px;
    overflow: hidden;
    position: relative;
    width: 100%;
  }
  
  .slide>div {
    animation: slidestay 16s 1;
    animation-fill-mode: forwards;
    background-position: center center;
    background-size: cover;
    height: 100%;
    opacity: 0;
    position: absolute;
    width: 100%;
  }
  
  @keyframes slidestay {
    10% {
      opacity: 1;
    }
    20% {
      opacity: 1;
    }
    30% {
      opacity: 1;
    }
    40% {
      transform: scale(1.2);
    }
    100% {
      opacity: 1;
    }
  }
}
</style>
<div class="slide">
  <div style="background-image:url(https://images.unsplash.com/photo-1575932150875-d31ebc835f67?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1650&q=80)"></div>
</div>

如果只想显示一张图片的动画。首先,删除您不想显示的 div。然后,您需要将 infinite 替换为 forwards 1。最后调整你的@keyframes,将“opacity”和“transform”设置为100%,并将值设置为你希望图像保持的方式。

  .slide {
  background: #fff;
  height: 350px;
  overflow: hidden;
  position: relative;
  width: 100%;
}

.slide>div {
  animation: slide 10s forwards 1; /* Play animation only once */
  background-position: center center;
  background-size: cover;
  height: 100%;
  opacity: 0;
  position: absolute;
  width: 100%;
}

@keyframes slide {
  100% {
    opacity: 1;
  }
  100% {
    transform: scale(1.2);
  }
}
<div class="slide">
  <div style="background-image:url(https://images.unsplash.com/photo-1575932150875-d31ebc835f67?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1650&q=80)"></div>
</div>