【问题标题】:How can I make my slideshow repeating without JS?如何在没有 JS 的情况下让我的幻灯片重复播放?
【发布时间】:2021-04-14 10:22:17
【问题描述】:

我目前正在尝试让我的图像幻灯片重复播放,以便在幻灯片结束时跳回开头或与第一张图像一起重复。不知何故,它不起作用,我不明白。

这是我的演示:

#logo-gallery-wrapper {
  overflow: hidden;
  position: relative;
}

#logo-gallery {
  animation: scroll-left 220s linear infinite;
  animation-iteration-count: infinite;
  margin: 0;
  padding: 0;
  position: relative;
  list-style-type: none;
  display: flex;
}

#logo-gallery .logo-gallery-figure {
  margin: 0;
  padding: 0 1.6rem;
  overflow: hidden;
}

#logo-gallery .logo-gallery-figure img {
  height: auto;
  max-height: 50px;
  position: relative;
  filter: grayscale(1);
  transition: all .4s;
}

#logo-gallery .logo-gallery-figure img:hover {
  filter: grayscale(0);
}

@keyframes scroll-left {
  0% {
    transform: translateX(0)
  }
  100% {
    transform: translateX(calc(-1000rem + 100vw))
  }
}
<div id="logo-gallery-wrapper">
  <ul id="logo-gallery">
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
  </ul>
</div>

【问题讨论】:

  • calc() 函数似乎不支持模数运算,这本来是我的建议。有一个用最少的 JS here 的答案。
  • 你确定不想在这里使用任何 JS 吗?没有它,您将复制您的元素并制作一个 hacky 关键帧动画,在结束/开始时交换它们。
  • 它似乎正在工作......只是你给了动画一个 220 秒的时间来执行,所以需要等待很长时间才能重新开始。另外,计算可能不太准确。 (您可以将动画更改为animation: scroll-left 2s linear infinite; 以查看它是否正常工作)
  • @SomoKRoceS 嗯,我明白了。但在这种情况下,我的客户不会因为阅读速度而很高兴。
  • 这样的话,速度还可以。您也许应该以不同的方式计算翻译,因为它在 220 秒结束之前就消失了。

标签: html css


【解决方案1】:

它似乎正在工作。只是你给了动画一个 220 秒的时间来执行,所以需要等那么久才能重新开始。

你应该修正翻译的计算,因为它在 220 秒结束之前就消失了。

您的最终关键帧应该是当前元素的宽度,您可以在渲染后检索它,并且需要javascript。你可以试试-100vw,但也不准确。

另外,我将速度降低到20s,但如果您愿意,可以降低速度。 其他一切都很好。 infinite 会做你想做的事。

运行这个 sn-p:

#logo-gallery-wrapper {
  overflow: hidden;
  position: relative;
}

#logo-gallery {
  animation: scroll-left 20s linear infinite;
  animation-iteration-count: infinite;
  margin: 0;
  padding: 0;
  position: relative;
  list-style-type: none;
  display: flex;
}

#logo-gallery .logo-gallery-figure {
  margin: 0;
  padding: 0 1.6rem;
  overflow: hidden;
}

#logo-gallery .logo-gallery-figure img {
  height: auto;
  max-height: 50px;
  position: relative;
  filter: grayscale(1);
  transition: all .4s;
}

#logo-gallery .logo-gallery-figure img:hover {
  filter: grayscale(0);
}

@keyframes scroll-left {
  0% {
    transform: translateX(0)
  }
  100% {
    transform: translateX(-100vw)
  }
}
<div id="logo-gallery-wrapper">
  <ul id="logo-gallery">
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
  </ul>
</div>

在您的 commnet 之后编辑:使用 Javascript

我评论了 sn-p 中的所有内容,以便清楚。但基本上,我在计算后使用width在里面创建了关键帧。

document.getElementsByTagName("body")[0].onload = createAnimation;

  function createAnimation(){

  let e = document.getElementById("logo-gallery"); // Get the element

  var style = document.createElement('style'); // Create styling element
  style.type = 'text/css'; // Append a css type

  // Now create the dynamic keyFrames (that are depend on logo-gallery final width)
  // Notice that the width of e is given to translateX at 100%
  let keyframes = '\
  @keyframes scroll-left {\
      0% {\
          transform: translateX(0);\
      }\
      100% {\
          transform: translateX(-'+e.scrollWidth+'px);\
      }\
  }';
  style.innerHTML = keyframes; // Set innerHTML of the styling element to the keyframe
  document.getElementsByTagName('head')[0].appendChild(style); // append the element to the head of the document as a stylesheet
  e.setAttribute("style","animation: scroll-left 20s linear infinite; animation-iteration-count: infinite;"); // Give the element its animation properties.

}
#logo-gallery-wrapper {
  overflow: hidden;
  position: relative;
}

#logo-gallery {
  margin: 0;
  padding: 0;
  position: relative;
  list-style-type: none;
  display: flex;
}

#logo-gallery .logo-gallery-figure {
  margin: 0;
  padding: 0 1.6rem;
  overflow: hidden;
}

#logo-gallery .logo-gallery-figure img {
  height: auto;
  max-height: 50px;
  position: relative;
  filter: grayscale(1);
  transition: all .4s;
}

#logo-gallery .logo-gallery-figure img:hover {
  filter: grayscale(0);
}
<div id="logo-gallery-wrapper">
  <ul id="logo-gallery">
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
    <li>
      <figure class="logo-gallery-figure">
        <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
      </figure>
    </li>
  </ul>
</div>

【讨论】:

  • 感谢您的帮助!它正在工作,但正如你所说,它不是 100% 完美的。结束后直接重新启动动画的正确方法是什么?
  • @Mr.Jo 不客气。我已经使用 JavaScript 选项编辑了我的答案,该选项完全符合您的要求。
  • 感谢您的帮助和负担。我真的很感激!
【解决方案2】:

它是因为图像无限向左滚动,并且在循环没有图像时它没有任何东西可以显示它只是显示空白。所以为了解决这个问题,我增加了图像的数量并现在对 css 文件进行了一些更改你的代码是这样的:

body{
    display: flex;
    justify-content: center;
    align-items: center;
}

#logo-gallery-wrapper {
    width: 75%;
    height: 5rem;
    overflow: hidden;
    position: relative;
    border: 1px solid red;
    display: flex;
    justify-content: flex-start;
    align-items: center;
  }
  
  #logo-gallery {
    animation: scroll-left 2s infinite linear;
    /* animation-iteration-count: infinite; */
    margin: 0;
    padding: 0;
    position: relative;
    list-style-type: none;
    display: flex;
  }
  
  #logo-gallery .logo-gallery-figure {
    margin: 0;
    padding: 0 1.6rem;
    overflow: hidden;
  }
  
  #logo-gallery .logo-gallery-figure img {
    height: auto;
    max-height: 50px;
    position: relative;
    filter: grayscale(1);
    transition: all .4s;
  }
  
  #logo-gallery .logo-gallery-figure img:hover {
    filter: grayscale(0);
  }
  
  @keyframes scroll-left {
    /* 0% {
      transform: translateX(0)
    } */
    100% {
      /* transform: translateX(calc(-1000rem + 100vw)) */
      transform: translateX(-100vw)
    }
  }
<div id="logo-gallery-wrapper">
        <ul id="logo-gallery">
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
          <li>
            <figure class="logo-gallery-figure">
              <img src="https://www.ikea.com/de/de/static/ikea-logo.f88b07ceb5a8c356b7a0fdcc9a563d63.svg">
            </figure>
          </li>
        </ul>
      </div>

【讨论】:

  • 但是我总是推荐大家用js来做这种编辑
猜你喜欢
  • 2013-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-01
相关资源
最近更新 更多