在视口中运行CSS关键帧动画

时间:2019-10-31 14:03:39

标签: javascript jquery html css

我正在尝试建立一个网站,并且我有一些关键帧动画来淡化文本。但是,动画完全在网站的开头运行。我要做的是仅在内容位于视口中时运行动画。只能在CSS和HTML中这样做吗?或者,如果我需要JavaScript,怎么办?

这是我到目前为止所拥有的:

我的HTML

<div class="section_container">
  <div style="background-color:red" class="section">
    <h1 class="animate_fadeup">Page1</h1>
  </div>
  <div style="background-color:green" class="section">
    <h1 class="animate_fadeup">Page2</h1>
  </div>
  <div style="background-color:blue" class="section">
    <h1 class="animate_fadeup">Page3</h1>
  </div>
</div>

我的CSS

.section_container {
  width: 100%;
  max-height: 100vh;
  overflow-y: scroll;
  border: 1px solid gray;
  scroll-snap-type: y mandatory;
  &.proximity {
    scroll-snap-type: y proximity;
  }
}
.section {
  border-bottom: 1px solid white;
  height: 100vh;
  font-size: 1.4rem;
  color: rgba(white, .5);
  text-align: center;
  scroll-snap-align: start;
  display: flex;
  flex-flow: column;
  justify-content: start;
}

/* Animations */
@keyframes fadeup {
  0% {
    bottom: -50px;
    opacity: 0.1;
  }
  100% {
    bottom: 0px;
    opacity: 1;
  }
}
.animate_fadeup {
  animation-name: fadeup;
  animation-duration: 3s;
  animation-fill-mode: both;
  position: relative;
}

或查看我的代码笔: https://codepen.io/steven97102/pen/poodwYR

0 个答案:

没有答案