【问题标题】:positioning of an SVG based on the animation SVG基于动画 SVG 的 SVG 定位
【发布时间】:2018-06-05 22:57:12
【问题描述】:

我试图让submarine (SVG) 看起来好像漂浮在一个 波(也是 SVG)。

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

这是wave的代码

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

【问题讨论】:

    标签: javascript html css animation svg


    【解决方案1】:

    我添加了一个名为“sub”的类来格式化您的图片并添加动画。请注意,我添加了“缓出”作为动画计时功能,以便它快速下降但上升较慢以跟上波浪计时。但这也只是调整参数和动画“updown”恰到好处的结果,使其根据波浪的开始方式以与波浪相反的方向工作。这是一种方法,如果您想调整高度或缓出,只需更改“子”类中缓出的秒数或调整 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>

    【讨论】:

    • 谢谢!我从来没有想过将潜艇的动画与海浪的动画同步。
    • 如果这回答了您的问题,如果您将其标记为已接受,我将不胜感激。否则,让我知道如何改进答案。谢谢!
    • 我看到sn-p全屏的时候代码不同步——是不是因为它使用了px?
    • 是的,您很抱歉我更改了它以使整页的时间更好。我还更改了 .sub 类的曲线。作为参考,如果你想要某个动画定时功能,这个网站很有用:css-tricks.com/ease-out-in-ease-in-out我选择了ease-in-out,因为sub需要在端点处缓慢以匹配波浪的运动。调整正确时间的最简单方法是 (@keyframes updown) 部分中的 % 参数。我将其从 50% 更改为 70%,以便更好地适应波浪。这对波浪和船的同步影响最大。
    • 经过您的修改,相信您已经解决了我的问题。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 2020-10-03
    • 2023-03-26
    • 2014-10-27
    相关资源
    最近更新 更多