css 无限水平滚动图像循环?

vd2z7a6w  于 2023-01-27  发布在  其他
关注(0)|答案(3)|浏览(157)

所以我尝试在我的网站上创建一个无限滚动动画,但是我真的很挣扎。原来的教程在这里,使用了6张图片,4张在最后重复,以使过渡无缝。
https://designshack.net/articles/css/infinitephotobanner/
问题是,当我添加更多的图片,动画不工作。我知道这是因为我需要增加宽度和其他变量。在原来的,它看起来像因为她有10张图片(6个原始和4个重复),每个是350px,并且照片横幅是3550px,公式应该是10倍的图像宽度加上50px的边距。所以我试着这样开始。
我一直在不停地调整横幅移动的距离,但是教程并没有解释我需要如何计算横幅移动的距离,而不使其循环。网上有很多人都有同样的问题,除了复制和粘贴别人的代码,我还没有找到任何明确的答案。有没有更好的指南,我可以使用,或者谁能告诉我需要调整哪些变量
另外,如果我将容器宽度从1000px更改为更大的值,我是否也必须调整其他数值?如果是这样,我如何计算?肯定有比重新查看循环1000次更好的方法,每次都稍微更改数值,直到像素完美排列?如果是这样,这将花费我非常长的时间,因为我的循环是如此之长。
如果有帮助的话,每个图像是800px乘308px。这里是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;

}

}
lzfw57am

lzfw57am1#

这个解决方案不需要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>

添加这些规则后,鼠标悬停时动画停止,图像可以手动滚动:

#container:hover {
   overflow: auto;
}

#container:hover .photobanner {
   animation-play-state:paused;
}
omqzjyyz

omqzjyyz2#

希望这个能帮到你

$('.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>
2eafrhcq

2eafrhcq3#

这是我的建议-只CSS,但使用CSS变量。
有了这个,你可以把图像或任何内容,直到块高度是相同的。也可以控制图像之间的空间。
重要提示-您需要填充整个屏幕宽度。当元素比可用屏幕短时,它将不起作用。
同样重要的是-你需要把所有可见的元素放在整个集合之后。我的意思是:
让我们说,当我们有7个项目,但我们希望只有3个是可见的一次(不超过这一点),所以在这7个项目之后,我们需要把前3个项目的副本从列表中。

  • 抱歉我的语言,希望你能理解我 *
.scroller__wrapper {
  /* How long one slide is visible on screen (from entering screen to leaving it) */
  --scrolling-gallery-item-duration: 5s;
  /* How many items we want to see on screen at once */
  --scrolling-gallery-items-visible: 7;
  /* How many items are to scroll */
  --scrolling-gallery-items-total: 7;
  margin-top: 2.25em;
  overflow: hidden;
  will-change: transform;
}

.scroller {
  animation-duration: calc(var(--scrolling-gallery-item-duration, 1s) / var(--scrolling-gallery-items-visible) * var(--scrolling-gallery-items-total));
  animation-timing-function: linear;
  animation-name: scrolling-gallery;
  animation-iteration-count: infinite;
  display: flex;
  white-space: nowrap;
}

.scroller__container {
  /* Without this, scroll will jump on desktop if any vertical scrollbar is shown */
  width: 100vw;
}

.scroller__item {
  flex: 1 0 calc(100% / var(--scrolling-gallery-items-visible));
  /* Without this, block elements will take width from their contents and thus making wrong calculations,
     so this just force elements to take only exact part of the container (screen) and equal for all */
  width: 0px;
  /* If you want to have it continuous without any spaces, remove two lines below */
  box-sizing: border-box;
  padding: 0.5em;
}

.scroller img {
  display: block;
  height: 100%;
  object-fit: cover;
  object-position: center;
  width: 100%;
}

@keyframes scrolling-gallery {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(calc(var(--scrolling-gallery-items-total) * -100vw / var(--scrolling-gallery-items-visible)));
  }
}

.scroller:hover,
.scroller:focus {
  animation-play-state: paused;
}

/* Styles only for switching */

input[type=radio] {
  display: none;
}

label {
  align-items: center;
  background-color: rgba(0, 192, 0, 1);
  cursor: pointer;
  display: inline-flex;
  flex-flow: column;
  justify-content: center;
  margin-right: 0.5em;
  margin-top: 1em;
  min-width: 2em;
  padding: 0.5em;
}

input#items-1:checked~.scroller__wrapper {
  --scrolling-gallery-items-visible: 1;
}

input#items-2:checked~.scroller__wrapper {
  --scrolling-gallery-items-visible: 2;
}

input#items-3:checked~.scroller__wrapper {
  --scrolling-gallery-items-visible: 3;
}

input#items-4:checked~.scroller__wrapper {
  --scrolling-gallery-items-visible: 4;
}

input#items-5:checked~.scroller__wrapper {
  --scrolling-gallery-items-visible: 5;
}

input#items-6:checked~.scroller__wrapper {
  --scrolling-gallery-items-visible: 6;
}

input#items-7:checked~.scroller__wrapper {
  --scrolling-gallery-items-visible: 7;
}

input#dur-1s:checked~.scroller__wrapper {
  --scrolling-gallery-item-duration: 1s;
}

input#dur-2s:checked~.scroller__wrapper {
  --scrolling-gallery-item-duration: 2s;
}

input#dur-3s:checked~.scroller__wrapper {
  --scrolling-gallery-item-duration: 3s;
}

input#dur-5s:checked~.scroller__wrapper {
  --scrolling-gallery-item-duration: 5s;
}

input#dur-10s:checked~.scroller__wrapper {
  --scrolling-gallery-item-duration: 10s;
}
<!-- Buttons for changing items -->
<label for="items-1">1</label>
<label for="items-2">2</label>
<label for="items-3">3</label>
<label for="items-4">4</label>
<label for="items-5">5</label>
<label for="items-6">6</label>
<label for="items-7">7</label>
<br />
<label for="dur-1s">1s</label>
<label for="dur-2s">2s</label>
<label for="dur-3s">3s</label>
<label for="dur-5s">5s</label>
<label for="dur-10s">10s</label>

<input name="items" id="items-1" type="radio">
<input name="items" id="items-2" type="radio">
<input name="items" id="items-3" type="radio">
<input name="items" id="items-4" type="radio">
<input name="items" id="items-5" type="radio">
<input name="items" id="items-6" type="radio">
<input name="items" id="items-7" type="radio">

<input name="dur" id="dur-1s" type="radio">
<input name="dur" id="dur-2s" type="radio">
<input name="dur" id="dur-3s" type="radio">
<input name="dur" id="dur-5s" type="radio">
<input name="dur" id="dur-10s" type="radio">

<!-- Scroller -->
<section class="scroller__wrapper">
  <div class="scroller__container">
    <div class="scroller">
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <!-- Here are exact copy of previous items -->
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
      <div class="scroller__item"><img src="https://via.placeholder.com/350x700"></div>
    </div>
  </div>
</section>

相关问题