【问题标题】:Infinite horizontal scrolling image loop?无限水平滚动图像循环?
【发布时间】:2020-08-17 23:56:09
【问题描述】:

所以我试图在我的网站上创建一个无限滚动动画,但我真的很挣扎。原始教程在这里,使用 6 张图片,最后重复 4 张以使过渡无缝。

https://designshack.net/articles/css/infinitephotobanner/

问题是当我添加更多图像时,动画不起作用。我知道那是因为我需要增加宽度和其他变量。在原版中,看起来因为她有 10 张图片(6 张原始图片和 4 张重复图片),每张 350 像素,并且照片横幅为 3550 像素,所以公式应该是图片宽度的 10 倍加上 50 像素的边距。所以我试着这样做开始。

我一直在调整横幅不断移动的幅度,但本教程没有解释我需要如何计算横幅在不循环的情况下需要移动的距离。在网上看有很多人有同样的问题,除了复制和粘贴某人的代码之外,我还没有找到任何明确的答案。有没有更好的指南可供我使用,或者任何人都可以让我知道我需要调整哪些变量?

另外,如果我将容器宽度从 1000 像素更改为更大的值,我是否也必须调整其他数字?如果是这样,我该如何计算?肯定有比重新观看循环 1000 次更好的方法,并且每次都稍微改变数字直到像素完美排列?如果是这样,那将需要我很长时间,因为我的循环太长了。

如果有帮助,每张图片都是 800 像素 x 308 像素。这是HTML。任何帮助甚至是学习如何自己解决问题的资源都将不胜感激。

<div id="container">

<div class="photobanner">

<img class="first" src="img1.png" alt="" />

<img src="img2.png" alt="" />

<img src="img3.png" alt="" />

<img src="img4.png" alt="" />

<img src="img5.png" alt="" />

<img src="img6.png" alt="" />

<img src="img7.png" alt="" />

<img src="img8.png" alt="" />

<img src="img9.png" alt="" />

<img src="img10.png" alt="" />

<img src="img11.png" alt="" />

<img src="img12.png" alt="" />

<img src="img13.png" alt="" />

<img src="img14.png" alt="" />

<img src="img1.png" alt="" />

<img src="img2.png" alt="" />

<img src="img3.png" alt="" />

<img src="img4.png" alt="" />

</div>

</div>

这里是 CSS


* {margin: 0; padding: 0;}

body {

<!-- src: http://subtlepatterns.com/?p=1045 -->

background: url('dark_geometric.png');

}

#container {

width: 1000px;

overflow: hidden;

margin: 50px auto;

background: white;

}



/*header*/

header {

width: 800px;

margin: 40px auto;

}

header h1 {

text-align: center;

font: 100 60px/1.5 Helvetica, Verdana, sans-serif;

}

header p {

font: 100 15px/1.5 Helvetica, Verdana, sans-serif;

text-align: justify;

}

/*photobanner*/

.photobanner {

height: 233px;

width: 14450px;

margin-bottom: 80px;

}



/*keyframe animations*/

.first {

-webkit-animation: bannermove 30s linear infinite;

-moz-animation: bannermove 30s linear infinite;

-ms-animation: bannermove 30s linear infinite;

-o-animation: bannermove 30s linear infinite;

animation: bannermove 30s linear infinite;

}

u/keyframes "bannermove" {

0% {

margin-left: 0px;

}

100% {

margin-left: -12000px;

}

}

u/-moz-keyframes bannermove {

0% {

margin-left: 0px;

}

100% {

margin-left: -12000px;

}

}

u/-webkit-keyframes "bannermove" {

0% {

margin-left: 0px;

}

100% {

margin-left: -12000px;

}

}

u/-ms-keyframes "bannermove" {

0% {

margin-left: 0px;

}

100% {

margin-left: -12000px;

}

}

u/-o-keyframes "bannermove" {

0% {

margin-left: 0px;

}

100% {

margin-left: -12000px;

}

}

【问题讨论】:

  • 更好的方法是使用 owl-carousel.. 然后你可以轻松实现这一点

标签: html css


【解决方案1】:

此解决方案无需 javascript 代码即可工作。

photobanner 具有绝对位置,因此它在正常流程之外,将white-space 设置为nowrap 会创建一个包含所有图像的水平容器。

为了创建无限循环,我插入了 4 张图片两次,动画将 photobanner 容器从尺寸的 0% 移动到 -50%。

由于是绝对位置,需要设置#container的高度。

如果不能两次添加图片,可以根据最后添加的图片数量来玩百分比,一定要足够填满#container的可见空间。

#container {
  height:350px; 
  position:relative; 
  overflow:hidden;
}

.photobanner {
  position:absolute; 
  top:0px; 
  left:0px; 
  overflow:hidden; 
  white-space: nowrap;
  animation: bannermove 10s linear infinite;
}

.photobanner img {    
  margin: 0 0.5em 
}

@keyframes bannermove {
  0% {
      transform: translate(0, 0);
  }
  100% {
      transform: translate(-50%, 0);
  }
}
<div id="container">
  <div class="photobanner">
    <img src="https://i.stack.imgur.com/xckZy.jpg" alt=""><img src="https://i.stack.imgur.com/CVgbr.jpg" alt=""><img src="https://i.stack.imgur.com/7c4yC.jpg" alt=""><img src="https://i.stack.imgur.com/RTiml.jpg" alt=""
><img src="https://i.stack.imgur.com/xckZy.jpg" alt=""><img src="https://i.stack.imgur.com/CVgbr.jpg" alt=""><img src="https://i.stack.imgur.com/7c4yC.jpg" alt=""><img src="https://i.stack.imgur.com/RTiml.jpg" alt="">
  </div>
</div>

【讨论】:

  • 为什么你多次使用同一张图片?
  • 因为动画将图像从总大小的 0 到 50% 向左移动,所以前 4 个图像消失了,接下来的 4 个图像显示出来。完成了动画,它快速地回到原来的位置并重新开始。
  • 我明白你的意思,但我有一个疑问。我有 10 张图片,我必须再次添加一些图片 2 次循环?
  • 正如我在答案中所写,您可以根据最后添加的图像数量来调整百分比,这必须足以填充#container 的可见空间。如果有 10 张图片,而动画开始时只有 3 张可见,则可以添加 3 张图片(前三张)并将百分比更改为 76.9% 左右
【解决方案2】:

希望这篇文章对你有帮助

$('.owl-carousel').owlCarousel({
  loop: true,
  margin: 0,
  nav: true,
  navText: [
    "<i class='fa fa-caret-left'></i>",
    "<i class='fa fa-caret-right'></i>"
  ],
  autoplay: true,
  autoplayHoverPause: true,
  responsive: {
    0: {
      items: 3
    },
    600: {
      items: 3
    },
    1000: {
      items: 5
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.1.3/assets/owl.carousel.min.css" rel="stylesheet"/>
<link href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>



<div class="container-fluid">
  <div class="owl-carousel owl-theme">
    <div class="item">
      <img  style="width:100%; height: 200px;  " src="https://source.unsplash.com/random" alt="">
    </div>
    <div class="item">
      <img  style="width:100%; height: 200px;  " src="https://source.unsplash.com/random" alt="">
    </div>
    <div class="item">
      <img  style="width:100%; height: 200px;  " src="https://source.unsplash.com/random" alt="">
    </div>
    <div class="item">
      <img  style="width:100%; height: 200px;  " src="https://source.unsplash.com/random" alt="">
    </div>
    <div class="item">
      <img  style="width:100%; height: 200px;  " src="https://source.unsplash.com/random" alt="">
    </div>
  </div>
</div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.1.3/owl.carousel.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

【讨论】:

    猜你喜欢
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 2017-02-09
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多