CSS animate through ul items

Viewed 130

I have an unordered list and I would like to loop through each list item 1 by 1. Currently I have a semi working solution but if you look closely you can see when it gets to "item 4" it begins to overlap with "item 1" and is inconsistent.

@keyframes fadein {
  0% {
    opacity: 0;
  }
  66% {
    opacity: 0;
  }
  76% {
    opacity: 1;
  }
  100% {
    opacity: 1;
  }
}

li {
  list-style: none;
  padding: 0;
  font-size: 40px;
  position: absolute;
  width: 100%;
  opacity: 0;
}

li:nth-of-type(1) {
  animation: fadein 6s ease-in-out -4s infinite alternate;
}

li:nth-of-type(2) {
  animation: fadein 6s ease-in-out 0s infinite alternate;
}

li:nth-of-type(3) {
  animation: fadein 6s ease-in-out 4s infinite alternate;
}

li:nth-of-type(4) {
  animation: fadein 6s ease-in-out 8s infinite alternate;
}
<ul class="usp__list">
  <li class="usp__item">
    Item 1
  </li>
  <li class="usp__item">
    Item 2
  </li>
  <li class="usp__item">
    Item 3
  </li>
  <li class="usp__item">
    Item 4
  </li>
</ul>

any help in working out how to resolve this would be great. I presume its something to do with the delay but after trying many variations I still have this underlying problem.

Fiddle - https://jsfiddle.net/15c4rd8v/

1 Answers

Your animation-duration is too short. You have an animation-delay of up to 8s but the duration is only 6s - therefor the overlapping of 2s. If you set the duration to 8s and change your keyframes animation from ...66% {...} 76%... to 75% {...} 80%... it works.

@keyframes fadein {
  0% {
    opacity: 0;
  }
  75% {
    opacity: 0;
  }
  80% {
    opacity: 1;
  }
  100% {
    opacity: 1;
  }
}

li {
  list-style: none;
  padding: 0;
  font-size: 40px;
  position: absolute;
  width: 100%;
  opacity: 0;
}

li:nth-of-type(1) {
  animation: fadein 8s ease-in-out -4s infinite alternate;
}

li:nth-of-type(2) {
  animation: fadein 8s ease-in-out 0s infinite alternate;
}

li:nth-of-type(3) {
  animation: fadein 8s ease-in-out 4s infinite alternate;
}

li:nth-of-type(4) {
  animation: fadein 8s ease-in-out 8s infinite alternate;
}
<ul class="usp__list">
  <li class="usp__item">
    Item 1
  </li>
  <li class="usp__item">
    Item 2
  </li>
  <li class="usp__item">
    Item 3
  </li>
  <li class="usp__item">
    Item 4
  </li>
</ul>

Related