Brontes
Brontes

Reputation: 35

Recreating aniamtion / side scroll slideshow

I'm trying to recreate a animation / side scroll effect. The effect is from https://elrond.com/#partners and its located in the partner section. The slow moving circles. I tried to recreate it, but after slamming my head against mine desk for the duration of the weekend I couldn't recreate it. So here I'm asking for some help.

I made a little code pen to show where I'm at now.

    .slide-wrapper{
  max-width: 800px; 
  height: 30vh;
  position: relative;
  margin: 0 auto;
  transform: translate3d(0,0,0);  
  outline: red;
}

.slider{   
  position: absolute;
  top: 0;
  left: 0;
  height: 200px;
  display:flex;
  transform: translate3d(0,0,0); 
  animation: scroll 15s infinite linear;  
}

.slider:last-child  .slide{
  background-color: steelblue;
  animation: scroll 13s infinite linear;
  margin-top: 5rem;  
}

.slide{  
  background-color:orangered;
  height:50px;
  width:50px;
  margin: 1rem 6rem;  
  
}

@keyframes scroll {  
  100%{transform: translateX(-66.6666%)}
}

https://codepen.io/tijmenjacobs/pen/LYxrvKq

My main questions are the following:

Hope you guys could help!

Thanks in advance!

Upvotes: 1

Views: 177

Answers (1)

Reyno
Reyno

Reputation: 6505

To get the desired effect you need two of the same sliders next to eachother (slider-main and slider-copy in the snippet).

I used CSS variables to make it easier to calculate offsets and whitespace. The offset between the slides is based on the amount per row (--slides-per-row). These are set within the HTML so you can easily reuse the slider somewhere else.

/* Basic reset */
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body > * + * {
  margin-top: 2em;
}

/* Slider styling */

.sliders {
  display: flex;
}

.slider {
  --slide-offset: calc(100vw / var(--slides-per-row, 4)); /* --slides-per-row is set via HTML and uses 4 if nothing is provided */
  --slide-size: 5rem;
  flex: 0 0 100vw;
  animation: scroll 10s linear infinite;
}

.slider-row {
  display: flex;
}

/* Offset every odd row */
.slider-row:nth-child(2n+1) {
  transform: translateX(
    calc((var(--slide-offset) / 2 + var(--slide-size) / 2) * -1)
  );
  margin-bottom: 2rem;
}

.slide {
  width: var(--slide-size);
  height: var(--slide-size);
  margin: 0 0 0 var(--slide-offset);
  background: steelblue;
  font-size: 2rem;
  color: white;
  /* Center content */
  display: flex;
  align-items: center;
  justify-content: center;
}

@keyframes scroll {
  100% {
    transform: translateX(-100%);
  }
}
<h2>4 slides per row</h2>
<div class='sliders' style='--slides-per-row: 4'>
  <div class='slider slider-main'>
    <div class='slider-row'>
      <div class='slide'>1</div>
      <div class='slide'>2</div>
      <div class='slide'>3</div>
      <div class='slide'>4</div>
    </div>
    <div class='slider-row'>
      <div class='slide'>5</div>
      <div class='slide'>6</div>
      <div class='slide'>7</div>
      <div class='slide'>8</div>
    </div>
  </div>
  
  <div class='slider slider-copy'>
    <div class='slider-row'>
      <div class='slide'>1</div>
      <div class='slide'>2</div>
      <div class='slide'>3</div>
      <div class='slide'>4</div>
    </div>
    <div class='slider-row'>
      <div class='slide'>5</div>
      <div class='slide'>6</div>
      <div class='slide'>7</div>
      <div class='slide'>8</div>
    </div>
  </div>
</div>

<h2>2 slides per row</h2>
<div class='sliders' style='--slides-per-row: 2'>
  <div class='slider slider-main'>
    <div class='slider-row'>
      <div class='slide'>1</div>
      <div class='slide'>2</div>
    </div>
    <div class='slider-row'>
      <div class='slide'>3</div>
      <div class='slide'>4</div>
    </div>
    <div class='slider-row'>
      <div class='slide'>5</div>
      <div class='slide'>6</div>
    </div>
    <div class='slider-row'>
      <div class='slide'>7</div>
      <div class='slide'>8</div>
    </div>
  </div>
  
  <div class='slider slider-copy'>
    <div class='slider-row'>
      <div class='slide'>1</div>
      <div class='slide'>2</div>
    </div>
    <div class='slider-row'>
      <div class='slide'>3</div>
      <div class='slide'>4</div>
    </div>
    <div class='slider-row'>
      <div class='slide'>5</div>
      <div class='slide'>6</div>
    </div>
    <div class='slider-row'>
      <div class='slide'>7</div>
      <div class='slide'>8</div>
    </div>
  </div>
</div>

Upvotes: 1

Related Questions