user5582011
user5582011

Reputation:

How to have a pure smooth CSS Text Slider?

I'm trying to build a pure CSS top bar slider for a webshop which shows the advantages of the shop. I got it working, but when the next slide fades in, the slide (the text) is overlapping the other text for a small amount of time and that doesn't look very smooth. Is there a way to prevent this with pure CSS? The slider is only made for mobile devices (<992px)

The best would be if the slides could slide-out to the left and get slided-in from the right, but I don't think this is possible with pure CSS.

How do I prevent the overlapping of text?

Any help is highly appreciated

Additional libraries loaded:

.trev-top-bar {
  background-color: #256dff;
  color: #fff;
  font-size: 14px;
  padding-top: 1rem;
  padding-bottom: 1rem;
}

.trev-top-bar .trev-top-bar-item:first-child {
  -webkit-animation: top-bar-animation-1 16s ease infinite;
  animation: top-bar-animation-1 16s ease infinite;
}

.trev-top-bar .trev-top-bar-item:nth-child(2) {
  -webkit-animation: top-bar-animation-2 16s ease infinite;
  animation: top-bar-animation-2 16s ease infinite;
}

.trev-top-bar .trev-top-bar-item:nth-child(3) {
  -webkit-animation: top-bar-animation-3 16s ease infinite;
  animation: top-bar-animation-3 16s ease infinite;
}

.trev-top-bar .trev-top-bar-item:nth-child(4) {
  -webkit-animation: top-bar-animation-4 16s ease infinite;
  animation: top-bar-animation-4 16s ease infinite;
}

.trev-top-bar .trev-top-bar-fa-icon,
.trev-top-bar .icon {
  color: #fff;
  margin-right: .3rem;
}

.trev-top-bar .trev-top-bar-fa-icon {
  font-size: 16px;
}

.trev-top-bar .icon svg {
  width: 16px;
  height: 16px;
}

@keyframes top-bar-animation-1 {
  0% {
    left: 0;
  }
  12.5% {
    left: 0;
  }
  25% {
    left: 100%;
  }
  37.5% {
    left: 100%;
  }
  50% {
    left: 100%;
  }
  62.5% {
    left: 100%;
  }
  75% {
    left: 100%;
  }
  87.5% {
    left: 100%;
  }
  95% {
    left: 0;
  }
}

@keyframes top-bar-animation-2 {
  0% {
    left: 100%;
  }
  12.5% {
    left: 100%;
  }
  25% {
    left: 0;
  }
  37.5% {
    left: 0;
  }
  50% {
    left: 100%;
  }
  62.5% {
    left: 100%;
  }
  75% {
    left: 100%;
  }
  87.5% {
    left: 100%;
  }
  100% {
    left: 100%;
  }
}

@keyframes top-bar-animation-3 {
  0% {
    left: 100%;
  }
  12.5% {
    left: 100%;
  }
  25% {
    left: 100%;
  }
  37.5% {
    left: 100%;
  }
  50% {
    left: 0;
  }
  62.5% {
    left: 0;
  }
  75% {
    left: 100%;
  }
  87.5% {
    left: 100%;
  }
  100% {
    left: 100%;
  }
}

@keyframes top-bar-animation-4 {
  0% {
    left: 100%;
  }
  12.5% {
    left: 100%;
  }
  25% {
    left: 100%;
  }
  37.5% {
    left: 100%;
  }
  50% {
    left: 100%;
  }
  62.5% {
    left: 100%;
  }
  75% {
    left: 0;
  }
  87.5% {
    left: 0;
  }
  100% {
    left: 100%;
  }
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="trev-top-bar overflow-hidden" style="height: 56px;">
  <div class="container-fluid
            ">
    <div class="row position-relative">
      <div class="col-12 col-md trev-top-bar-item text-center position-absolute">
        <i class="trev-top-bar-fa-icon fa-solid fa-truck-fast"></i> Fast Shipping
      </div>

      <div class="col-12 col-md trev-top-bar-item text-center position-absolute">
        <i class="trev-top-bar-fa-icon fa-solid fa-arrow-right-arrow-left"></i> 100 Days Right of Return
      </div>

      <div class="col-12 col-md trev-top-bar-item text-center position-absolute">
        <i class="trev-top-bar-fa-icon fa-solid fa-file-invoice-dollar"></i> Buy with Invoice
      </div>

      <div class="col-12 col-md trev-top-bar-item text-center position-absolute">
        <i class="trev-top-bar-fa-icon fa-solid fa-gears"></i> Proven Quality
      </div>
    </div>
  </div>
</div>

Upvotes: 1

Views: 1540

Answers (1)

Arleigh Hix
Arleigh Hix

Reputation: 10877

So you just need to move each item from left: 100%; (off to right) to left: 0; (showing) to left: -100% (off to left) at the end and leave it there. Then at the beginning of the next animation loop everything resets to the starting position left: 100%; without animation, so you don't see the move back, and you have a continuous right to left flow.
Here is a snippet to demonstrate:

.trev-top-bar {
  background-color: #256dff;
  color: #fff;
  font-size: 14px;
  padding-top: 1rem;
  padding-bottom: 1rem;
}

.trev-top-bar .trev-top-bar-item:first-child {
  -webkit-animation: top-bar-animation-1 16s ease infinite;
  animation: top-bar-animation-1 16s ease infinite;
}

.trev-top-bar .trev-top-bar-item:nth-child(2) {
  -webkit-animation: top-bar-animation-2 16s ease infinite;
  animation: top-bar-animation-2 16s ease infinite;
}

.trev-top-bar .trev-top-bar-item:nth-child(3) {
  -webkit-animation: top-bar-animation-3 16s ease infinite;
  animation: top-bar-animation-3 16s ease infinite;
}

.trev-top-bar .trev-top-bar-item:nth-child(4) {
  -webkit-animation: top-bar-animation-4 16s ease infinite;
  animation: top-bar-animation-4 16s ease infinite;
}

.trev-top-bar .trev-top-bar-fa-icon,
.trev-top-bar .icon {
  color: #fff;
  margin-right: .3rem;
}

.trev-top-bar .trev-top-bar-fa-icon {
  font-size: 16px;
}

.trev-top-bar .icon svg {
  width: 16px;
  height: 16px;
}

@keyframes top-bar-animation-1 {
  0% {
    left: 100%;
  }
  8.33% {
    left: 0;
  }
  16.66% {
    left: 0;
  }
  24.99% {
    left: -100%;
  }
  100% {
    left: -100%;
  }
}

@keyframes top-bar-animation-2 {
  0% {
    left: 100%;
  }
  24.99% {
    left: 100%;
  }
  33.33% {
    left: 0;
  }
  41.66% {
    left: 0;
  }
  49.99% {
    left: -100%;
  }
  100% {
    left: -100%;
  }
}

@keyframes top-bar-animation-3 {
  0% {
    left: 100%;
  }
  49.99% {
    left: 100%;
  }
  58.33% {
    left: 0;
  }
  66.66% {
    left: 0;
  }
  74.99% {
    left: -100%;
  }
  100% {
    left: -100%;
  }
}

@keyframes top-bar-animation-4 {
  0% {
    left: 100%;
  }
  74.99% {
    left: 100%;
  }
  83.33% {
    left: 0;
  }
  91.66% {
    left: 0;
  }
  100% {
    left: -100%;
  }
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="trev-top-bar overflow-hidden" style="height: 56px;">
  <div class="container-fluid">
    <div class="row position-relative">
      <div class="col-12 col-md trev-top-bar-item text-center position-absolute">
        <i class="trev-top-bar-fa-icon fa-solid fa-truck-fast"></i> Fast Shipping
      </div>

      <div class="col-12 col-md trev-top-bar-item text-center position-absolute">
        <i class="trev-top-bar-fa-icon fa-solid fa-arrow-right-arrow-left"></i> 100 Days Right of Return
      </div>

      <div class="col-12 col-md trev-top-bar-item text-center position-absolute">
        <i class="trev-top-bar-fa-icon fa-solid fa-file-invoice-dollar"></i> Buy with Invoice
      </div>

      <div class="col-12 col-md trev-top-bar-item text-center position-absolute">
        <i class="trev-top-bar-fa-icon fa-solid fa-gears"></i> Proven Quality
      </div>
    </div>
  </div>
</div>

Note: I have removed the un-needed, intermediary, keyframes where the position has no change.

Upvotes: 1

Related Questions