css动画从绝对变为相对

时间:2017-07-06 21:42:38

标签: css animation css-animations

我找到了这个波浪动画" https://codepen.io/tedmcdo/pen/PqxKXg"并希望在我的页面上的headerimg上使用它,但问题是动画是为绝对值编写的,但我需要一个相对位置,因为动画应该保持在页面顶部。

我已经尝试将位置类型更改为相对但是我这样做我打破动画,如果有人可以帮助我,我会很高兴^^

谢谢大家的时间。

这是css:

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

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

.wave {
  background: url(http://tedmcdo.com/labs/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);
  }
}

2 个答案:

答案 0 :(得分:2)

将它放入相对div(或其他容器)中,.ocean在相对容器中是绝对的。这可以解决你的问题。

<div style="position: relative">
   <div class="ocean" style="position: absolute"></div>
</div>

答案 1 :(得分:0)

不太清楚你的意思是什么:

...need a relative position since the animation should stay on top of the page.

......但我离题了。

添加包含相对定位的div

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

使用CSS

.oceanContainer {
    position: relative;
    height: 100% /* you can play around with this for your header */
}

您可能需要调整其他一些CSS

.ocean {
  height: 5%; /* as above, adjust to %, px, (r)em's to your liking */
  bottom: 0;
}

.wave {
  bottom: 0;    /* change top to bottom */
}

.wave:nth-of-type(2) {
  bottom: 0;    /* change top to bottom */
}