【问题标题】:Fade slideshow single image淡出幻灯片单张图像
【发布时间】:2021-08-23 23:44:05
【问题描述】:

我的主页上有一个渐变的幻灯片,循环播放 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);
    }
}

这是小提琴:https://jsfiddle.net/nd81hzuc/,它有四个图像:-)

感谢任何帮助!

谢谢,
乔什

【问题讨论】:

    标签: html css css-animations slideshow keyframe


    【解决方案1】:

    您可以使用此属性animation-iteration-count: 1; 来决定您的动画将运行多少次。您可以在&lt;div class="slide"&gt; 中使用单个图像,这样只有一张图像会循环通过@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);
        }
    }
    

    喜欢下面sn-p中的demo

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

    【讨论】:

    • 我喜欢这两种解决方案!!感谢您的帮助,我选择了另一个,因为它与我的原始代码最接近 :-) 只是想让您知道,我感谢您花时间伸出援手!
    • 任何时候社区都是为了互相帮助,但如果你喜欢答案,你可以投票赞成他们,只是说; )
    • 绝对,完全同意!
    【解决方案2】:

    如果您只想显示一张图片的动画。首先,删除您不想显示的 div。然后,您需要将infinite 替换为forwards 1。最后调整您的@keyframes,将“不透明度”和“变换”设置为 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>

    【讨论】:

    • 我喜欢这个简单的,当然!!
    【解决方案3】:

    您可以通过稍微调整为单个 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>

    【讨论】:

    • 我也喜欢这个解决方案,只是想我会选择最简单的答案 :-) 感谢分享!
    猜你喜欢
    • 2014-11-22
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 2017-10-11
    • 2010-11-24
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多