【问题标题】:CSS: repeated animated backgroundCSS:重复的动画背景
【发布时间】:2021-06-08 17:06:39
【问题描述】:

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

我得到的最好的是:

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)等等。

【问题讨论】:

    标签: css css-animations background-image linear-gradients


    【解决方案1】:

    可能如下:

    .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>

    【讨论】:

      【解决方案2】:

      更新:这是一个有趣的问题。我惊讶地发现我没有一个明显或特别优雅的解决方案来让渐变垂直运行,同时重复水平间隙。

      比我最初预期的要难以捉摸。

      我能想到的最好的办法是将其中一个渐变放在伪元素中并应用蒙版图像。这在 IE 中不起作用,但在其他任何地方都可以使用 appears to be supported

      请参阅下面的更新演示。


      如果我理解你想要做什么,我认为你可以通过为背景位置设置动画来完成它:

      .demo {  
        height: 200px;
        background-image: 
          linear-gradient(#f4e04d, #3bceac 20%, rgba(22, 22, 22, 0) 100%);
        animation: move 0.7s infinite alternate;
        background-size: 3rem;
        position: relative;
      }
      
      .demo::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        background: linear-gradient(#042a2b, transparent);
        
        /* This is the magic part: using a horizontal repeating-linear-gradient
        to mask out "columns", allowing the container's background gradient to
        show through */
        -webkit-mask-image: repeating-linear-gradient(to right, black 0 3rem, transparent 3rem 6rem);
        
        background-size: 3rem;
        
        /* run the same animation in reverse to animate up instead of down */
        animation: move 0.7s infinite alternate-reverse;
      }
      
      @keyframes move {
        from {
          background-position: 0 0;
        }
        to {
          background-position:
            0 200px;
        }
      }
      <div class="demo"></div>

      【讨论】:

      • 这非常接近我想要的,谢谢。我只是在努力使第二个渐变位于第一个渐变的右侧。所以我正在寻找的是以下模式:AbAbAb... A 向上移动,b 向下移动。然后 A 向下移动,B 向上移动。
      • 现在看起来像我的答案:)
      • 是的。在我进行编辑后,我注意到你做了基本相同的事情。但是您的重复/调整大小方法更好。干杯。
      【解决方案3】:

      很难准确推断出您想要做什么,但这里有另一个示例(与@ray hatfield's answer 非常相似),它将第一个背景向下移动,而第二个背景向上移动:

      .sample {
          width: 250px;
          height: 50px;
          position: relative;
          background-image: linear-gradient(to bottom, #f4e04d, #3bceac 20%, rgba(22, 22, 22, 0) 100%), linear-gradient(to right, #042a2b 3rem, transparent 3rem, transparent 6rem);
          animation: move 1s infinite linear;
      }
      @keyframes move {
        0%, 100% {
          background-position: 0 -75px, 0 0;
        }
        50% {
          background-position: 0 0, 0 -75px;
        }
      }
      <div class="sample"></div>

      【讨论】:

      • 谢谢,我添加了一张图片来说明我在寻找什么,因为它与这个有点不同。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2018-04-03
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      相关资源
      最近更新 更多