首页 > 解决方案 > CSS:重复的动画背景

问题描述

我正在尝试创建一个由两部分组成的重复背景。每个部分都是一个渐变,一个向上移动,另一个向下移动。

我得到的最好的是:

html {
  background: black;
  color: #4c4c4c;
}

body {
  margin: 30vh auto;
  max-width: 80vw;
}

.wave {
  background: none;
  height: 1rem;
  width: 50%;
  position: absolute;
  z-index: 2;
  animation: move 700ms 0ms steps(2) infinite both;
}

.color::after,
.color::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  height: 100%;
  top: 0;
}

.color {
  background-image: linear-gradient(#fe0000 50%, #6531ff 0 100%);
}

.color::after {
  background: linear-gradient(to bottom, #f4e04d, #3bceac 20%, rgba(22, 22, 22, 0) 100%), linear-gradient(to right, #042a2b 3rem, transparent 3rem, transparent 6rem);
}

.wave,
.color::after,
.color::before {
  background-size: 5rem 1rem;
  background-repeat: repeat-x;
}

@keyframes move {
  0% {
    margin-top: -3rem;
  }
  100% {
    margin-top: -3.25rem;
  }
}
<div class="color wave"></div>

我明白为什么这不起作用,但不知道如何继续。

由于很难描述,这是我正在寻找的图像:不同的职位

首先(位置 1),所有奇数块都高于偶数块。在第一个动画之后,它是相反的(位置 2)等等。

标签: csscss-animationsbackground-imagelinear-gradients

解决方案


可能像下面这样:

.box {
  height:100px;
  background:linear-gradient(red,blue,yellow,red) 0 0/100% 200%;
  animation:y 2s linear infinite;
}
.box::after {
  content:"";
  display:block;
  height:100%;
  background:linear-gradient(green,lightblue,pink,green) 0 0/100% 200%;
  animation:inherit;
  animation-direction: reverse;
  -webkit-mask:linear-gradient(90deg,#fff 50%,transparent 0) 0 0/20% 100%;
}

@keyframes y {
  to {
    background-position:0 -200%;
  }
}
<div class="box"></div>


推荐阅读