如何关闭盒子类动画

时间:2019-03-31 09:54:04

标签: html css css-animations

Box类与活塞类一起获得动画效果,但我希望它保持在一个位置。我不知道他如何继承这个动画或为什么。我尝试使用(动画:无!重要;),但没有用。

@keyframes piston {
  0%,
  100% {
    margin-top: 175px
  }
  50% {
    margin-top: 50px
  }
}

@keyframes handle {
  0%,
  100% {
    height: 225px
  }
  50% {
    height: 100px
  }
}

.piston {
  animation: piston 5s linear infinite;
  background-color: black;
  border: 3px solid black;
  width: 150px;
  height: 50px;
  margin-left: -68px
}

.handle {
  animation: handle 5s linear infinite;
  width: 25px;
  height: 225px;
  margin-left: 68px;
  border: 5px black solid;
  background-color: black;
}

.box {
  width: 156px;
  height: 200px;
  border: 3px solid red;
  border-top: none;
  margin-left: 1px;
}
<div class='handle'>
  <div class='piston'></div>
</div>
<div class='box'></div>

2 个答案:

答案 0 :(得分:4)

您可以通过在HTML中将具有div类的.box和具有div类的.handle放置在HTML中,并设置.box类在您的CSS中包含position: fixed

您的动画位置略有偏离,因此我也为您修复了该问题。您可以改回动画并调整框的高度以使其适合。

@keyframes piston {
  0%,
  100% {
    margin-top: 140px
  }
  50% {
    margin-top: 50px
  }
}

@keyframes handle {
  0%,
  100% {
    height: 175px
  }
  50% {
    height: 100px
  }
}

.piston {
  animation: piston 5s linear infinite;
  background-color: black;
  border: 3px solid black;
  width: 150px;
  height: 50px;
  margin-left: -68px
}

.handle {
  animation: handle 5s linear infinite;
  width: 25px;
  height: 225px;
  margin-left: 68px;
  border: 5px black solid;
  background-color: black;
}

.box {
  width: 156px;
  height: 200px;
  border: 3px solid red;
  border-top: none;
  margin-left: 2px;
  position: fixed;
}
<div class='box'></div>
<div class='handle'>
  <div class='piston'></div>
</div>

答案 1 :(得分:4)

您可以使用以下一种元素来简化动画:

.box {
  width: 156px;
  height: 200px;
  border: 3px solid red;
  border-top: none;
  position:relative;
  overflow:hidden;
}

.box:before {
  content:"";
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background:
    linear-gradient(#000,#000) center/25px 100% no-repeat;
  border-bottom:50px solid;
  box-sizing:border-box;
  animation:change 2s infinite alternate;
}
@keyframes change {
  from {
    transform:translateY(-60%);
  }
}
<div class='box'></div>

只有背景动画的另一个想法:

.box {
  width: 156px;
  height: 200px;
  border: 3px solid red;
  border-top: none;
  background:
    linear-gradient(#000,#000) bottom center/25px 100%,
    linear-gradient(#000,#000) bottom /100% 50px;
  background-repeat:no-repeat;
  background-origin:content-box;
  box-sizing:border-box;
  animation:change 2s infinite alternate;
}
@keyframes change {
  to {
    padding-bottom:120px;
  }
}
<div class='box'></div>