javascript - 关键帧动画不向后运行

标签 javascript html css animation css-animations

我正在创建动画效果(缓慢降低高度)作为关键帧动画的一部分。动画播放一次 forwards,但不播放 backwards 并且不会在后续时间播放。

JSFiddle 在这里 - http://jsfiddle.net/shaaraddalvi/eLwcw22e/

var scrollEventHandler = function() {
	if(window.scrollY > 210) {
     document.getElementById("banner-container").classList.remove("dynamic");
     document.getElementById("banner-container").classList.add("static");
  }
  else {
    if (document.getElementById("banner-container").classList.contains("static")) {
      document.getElementById("banner-container").classList.remove("static");
      document.getElementById("banner-container").classList.add("dynamic");
    }
  }
}

$(document).scroll(scrollEventHandler);
#header {
  height: 200px;
  padding: 5px;
  background-color: purple;
  color: white;
}

@-webkit-keyframes someanimation {
  from { padding: 100px 0px; }
  to { padding: 10px 0px; }
}
@-moz-keyframes someanimation {
  from { padding: 100px 0px; }
  to { padding: 10px 0px; }
}
@-ms-keyframes someanimation {
  from: { padding: 100px 0px; }
  to: { padding: 10px 0px; }
}

#banner-container {
  padding: 100px 0px;
  background-color: orange;
}

#banner-container.static {
  position: fixed;
  top: 0;
  width: 100%;
  -webkit-animation: someanimation 1s forwards;
  -moz-animation: someanimation 1s forwards;
  -ms-animation: someanimation 1s forwards;
}

#banner-container.dynamic {
  -webkit-animation: someanimation 1s backwards;
  -moz-animation: someanimation 1s backwards;
  -ms-animation: someanimation 1s backwards;
}

#banner {
  width: 500px;
  margin: 0 auto;
}

#buffer {
  background-color: green;
  padding: 50px;
  height:5000px;
}

@-webkit-keyframes bufferAnimation {
  from { padding-top: 268px; }
  to { padding-top: 88px; }
}
@-moz-keyframes bufferAnimation {
  from { padding-top: 268px; }
  to { padding-top: 88px; }
}
@-ms-keyframes bufferAnimation {
  from { padding-top: 268px; }
  to { padding-top: 88px; }
}

#banner-container.static + #buffer {
  -webkit-animation: bufferAnimation 1s forwards;
  -moz-animation: bufferAnimation 1s forwards;
  -ms-animation: bufferAnimation 1s forwards;
}

#banner-container.dynamic + #buffer {
  -webkit-animation: bufferAnimation 1s backwards;
  -moz-animation: bufferAnimation 1s backwards;
  -ms-animation: bufferAnimation 1s backwards;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header">
  This is header
</div>
<div id="banner-container">
 <div id="banner">
  Banner text
 </div>
</div>
<div id="buffer">
  Buffer text 1<br/>  
  Buffer text 2<br/>
  Buffer text 3<br/>
  Buffer text 4<br/>
  Buffer text 5<br/>
  Buffer text 6<br/>
  Buffer text 7<br/>
  Buffer text 8<br/>
  Buffer text 9<br/>
  Buffer text 10<br/>
  Buffer text 11<br/>
  Buffer text 12<br/>
  Buffer text 13<br/>
  Buffer text 14<br/>
  Buffer text 15<br/>
  Buffer text 16<br/>
  Buffer text 17<br/>
</div>

滚动页面以获得所需的效果。 (目前在 chrome 上测试,在 Edge 上效果不佳)。

最佳答案

您可以通过添加另一个关键帧动画来实现这一点:

var scrollEventHandler = function() {
	if(window.scrollY > 210) {
     document.getElementById("banner-container").classList.remove("dynamic");
     document.getElementById("banner-container").classList.add("static");
  }
  else {
    if (document.getElementById("banner-container").classList.contains("static")) {
      document.getElementById("banner-container").classList.remove("static");
      document.getElementById("banner-container").classList.add("dynamic");
    }
  }
}

$(document).scroll(scrollEventHandler);
#header {
  height: 200px;
  padding: 5px;
  background-color: purple;
  color: white;
}

@-webkit-keyframes someanimation {
  from { padding: 100px 0px; }
  to { padding: 10px 0px; }
}
@-moz-keyframes someanimation {
  from { padding: 100px 0px; }
  to { padding: 10px 0px; }
}
@-ms-keyframes someanimation {
  from: { padding: 100px 0px; }
  to: { padding: 10px 0px; }
}

@-webkit-keyframes someanimation2 {
  from { padding: 10px 0px; }
  to { padding: 100px 0px; }
}
@-moz-keyframes someanimation2 {
  from { padding: 10px 0px; }
  to { padding: 100px 0px; }
}
@-ms-keyframes someanimation2 {
  from: { padding: 10px 0px; }
  to: { padding: 100px 0px; }
}

#banner-container {
  padding: 100px 0px;
  background-color: orange;
}

#banner-container.static {
  position: fixed;
  top: 0;
  width: 100%;
  -webkit-animation: someanimation 1s forwards;
  -moz-animation: someanimation 1s forwards;
  -ms-animation: someanimation 1s forwards;
}

#banner-container.dynamic {
  -webkit-animation: someanimation2 1s backwards;
  -moz-animation: someanimation2 1s backwards;
  -ms-animation: someanimation2 1s backwards;
}

#banner {
  width: 500px;
  margin: 0 auto;
}

#buffer {
  background-color: green;
  padding: 50px;
  height:5000px;
}

@-webkit-keyframes bufferAnimation {
  from { padding-top: 268px; }
  to { padding-top: 88px; }
}
@-moz-keyframes bufferAnimation {
  from { padding-top: 268px; }
  to { padding-top: 88px; }
}
@-ms-keyframes bufferAnimation {
  from { padding-top: 268px; }
  to { padding-top: 88px; }
}

#banner-container.static + #buffer {
  -webkit-animation: bufferAnimation 1s forwards;
  -moz-animation: bufferAnimation 1s forwards;
  -ms-animation: bufferAnimation 1s forwards;
}

#banner-container.dynamic + #buffer {
  -webkit-animation: bufferAnimation 1s backwards;
  -moz-animation: bufferAnimation 1s backwards;
  -ms-animation: bufferAnimation 1s backwards;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header">
  This is header
</div>
<div id="banner-container">
 <div id="banner">
  Banner text
 </div>
</div>
<div id="buffer">
  Buffer text 1<br/>  
  Buffer text 2<br/>
  Buffer text 3<br/>
  Buffer text 4<br/>
  Buffer text 5<br/>
  Buffer text 6<br/>
  Buffer text 7<br/>
  Buffer text 8<br/>
  Buffer text 9<br/>
  Buffer text 10<br/>
  Buffer text 11<br/>
  Buffer text 12<br/>
  Buffer text 13<br/>
  Buffer text 14<br/>
  Buffer text 15<br/>
  Buffer text 16<br/>
  Buffer text 17<br/>
</div>

你也可以查看这个Fiddle

关于javascript - 关键帧动画不向后运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46233486/

相关文章:

javascript - 如何在javascript中开发正则表达式?

javascript - 使用 localstorage 持久化检查状态

javascript - 修改按钮单击时调用的 javascript 函数

css - 自动高度 md 按钮

Javascript 下划线总是返回 false

javascript - 映射文件更改时不要重新启动 IIS

javascript - 如何禁用 html 元素的悬停事件

javascript - 我可以通过 Javascript 向表单添加参数吗?

css - 当 div 使用边距 :auto;? 居中时,如何定位下拉导航菜单

javascript - 添加并应用带有按钮/链接的新 CSS 样式表