我试图使submarine(SVG)看起来好像漂浮在
波浪(也是SVG)。

由于波浪不断地上下波动,因此我希望潜水艇垂直居中(x),但要在波浪顶部水平移动。

这是波浪的代码



// best seen at 1500px or less

html, body { height: 100%; }
body {
  background:radial-gradient(ellipse at center, rgba(255,254,234,1) 0%, rgba(255,254,234,1) 35%, #B7E8EB 100%);
  overflow: hidden;
}

.ocean {
  height: 5%;
  width:100%;
  position:absolute;
  bottom:0;
  left:0;
  background: #015871;
}

.wave {
  background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/85486/wave.svg) repeat-x;
  position: absolute;
  top: -198px;
  width: 6400px;
  height: 198px;
  animation: wave 7s cubic-bezier( 0.36, 0.45, 0.63, 0.53) infinite;
  transform: translate3d(0, 0, 0);
}

.wave:nth-of-type(2) {
  top: -175px;
  animation: wave 7s cubic-bezier( 0.36, 0.45, 0.63, 0.53) -.125s infinite, swell 7s ease -1.25s infinite;
  opacity: 1;
}

@keyframes wave {
  0% {
    margin-left: 0;
  }
  100% {
    margin-left: -1600px;
  }
}

@keyframes swell {
  0%, 100% {
    transform: translate3d(0,-25px,0);
  }
  50% {
    transform: translate3d(0,5px,0);
  }
}

<div class="ocean">
  <div class="wave"></div>
  <div class="wave"></div>
</div>

最佳答案

我添加了一个名为“ sub”的类,以格式化图片并添加动画。注意,我添加了“ ease-out”作为动画定时功能,以便它能快速下降但较慢地上升以跟上波浪定时。但这仅仅是调整参数和正确调整动画“上下”的结果,以便动画根据其开始方向与波浪相反。这是一种实现方法,如果您想调整高度或缓和,只需在“ sub”类中更改缓动的秒数或在updown函数中调整像素数即可。希望这可以帮助!



// best seen at 1500px or less



 html, body { height: 100%; }
    body {
      background:radial-gradient(ellipse at center, rgba(255,254,234,1) 0%, rgba(255,254,234,1) 35%, #B7E8EB 100%);
      overflow: hidden;
    }


    .sub{
       position: absolute;
       top: 85%;
       left: 50%;
       width: 100px;
       height: 100px;
       margin-top: -100px; /* Start height margin */
       margin-left: -50px; /* Half the width */
       animation: updown 7s cubic-bezier(0.42, 0, 0.58, 1) infinite;
       animation-timing-function:ease-in-out;
       transform: translate3d(0, 0, 0);
     }

    .ocean {
      height: 5%;
      width:100%;
      position:absolute;
      bottom:0;
      left:0;
      background: #015871;
    }

    .wave {
      background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/85486/wave.svg) repeat-x;
      position: absolute;
      top: -198px;
      width: 6400px;
      height: 198px;
      animation: wave 7s cubic-bezier(0.36, 0.48, 0.63, 0.53) infinite;
      transform: translate3d(0, 0, 0);
    }

    .wave:nth-of-type(2) {
      top: -175px;
      animation: wave 7s cubic-bezier( 0.36, 0.45, 0.63, 0.53) -.125s infinite,
      swell 7s ease -1.25s infinite;
      opacity: 1;
    }

    @keyframes wave {
      0% {
        margin-left: 0;
      }
      100% {
        margin-left: -1600px;
      }
    }

    @keyframes swell {
      0%, 100% {
        transform: translate3d(0,-25px,0);
      }
      50% {
        transform: translate3d(0,5px,0);
      }
    }

        @keyframes updown {
      0%, 100% {
            transform: translate3d(0,-40px,0);
      }
      70% {
            transform: translate3d(0,45px,0);
      }
    }

<img class = "sub" src = "https://image.flaticon.com/icons/svg/447/447773.svg">
    <div class="ocean">
      <div class="wave"></div>
      <div class="wave"></div>
    </div>

10-07 12:52
查看更多