CSS关键帧:如何将div移动到特定位置

时间:2014-12-12 14:18:35

标签: css css3 css-animations

我有一个div,当我添加类删除时,我想移动到某个位置(例如[200,200])。我有很多来自不同地方的div,我希望他们在[200,200]见面。

  

.remove {-webkit-animation:swoopOut 2s 1轻松前锋; }

     

@ - webkit-keyframes swoopOut {

0% {position: relative; left: 0px; top: 00px; opacity: 1}
80% {position: absolute; left: 200px; top: 200px; opacity: 1}
100% {position: absolute; left: 200px; top: 200px; opacity: 0}
     

}

当我使用top / left时,它会相对移动(200向下,200向右)虽然我想要绝对(到[200,200])。我尝试过的位置:绝对但是那不起作用。

任何帮助?

编辑:我试图在fiddle做一个例子,我想要两个盒子在150,50见面。我该怎么办?

1 个答案:

答案 0 :(得分:1)

很多div s?使用CSS,我很确定如果没有每个元素的keyframe规则,那是不可能的。但是,使用JS:

window.onload = function() {
  TweenLite.to(document.getElementsByClassName("box"), 1, {
    top: 50,
    left: 150,
    onComplete: function() {
      TweenLite.to(this.target, 0.2, {
        opacity: 0,
        onComplete: function() {
          TweenLite.set(this.target, {
            opacity: 1
          });
          this.restart();
        }.bind(this)
      });
    }
  });
};
body {
  margin: 0px;
}
#container {
  position: relative;
}
.wrapper {
  /*position: relative;*/
  width: 100px;
  height: 100px;
  border: 1px solid black;
}
.meet {
  position: absolute;
  top: 50px;
  left: 150px;
  width: 100px;
  height: 100px;
  border: 1px solid black;
}
.box {
  position: absolute;
  width: 100px;
  height: 100px;
  background-color: coral;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/plugins/CSSPlugin.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenLite.min.js"></script>
<div id="container">
  <div class="wrapper">
    <div class="box"></div>
  </div>
  <div class="wrapper">
    <div class="box"></div>
  </div>
  <div class="meet"></div>
</div>

此处仅限CSS,但有多个keyframeanimation规则。

body {
    margin: 0px;
}

#container {
  position: relative;
}
.wrapper {
    position: relative;
    width: 100px;
    height: 100px;
    border: 1px solid black;
}
.meet {
    position: fixed;
    top: 50px;
    left: 150px;
    width: 100px;
    height: 100px;
    border: 1px solid black;
}

.box {
  position: absolute;
  width: 100px;
  height: 100px;
  background-color: coral;
}
.box1 {
  -webkit-animation: boxOne 1s infinite;
}
.box2 {
  -webkit-animation: boxTwo 1s infinite;
}
@-webkit-keyframes boxOne {
  0% {
    left: 0px;
    top: 0px;
    opacity: 1
  }
  80% {
    left: 150px;
    top: 50px;
    opacity: 1
  }
  100% {
    left: 150px;
    top: 50px;
    opacity: 0
  }
}
@-webkit-keyframes boxTwo {
  0% {
    left: 0px;
    top: 0px;
    opacity: 1
  }
  80% {
    left: 150px;
    top: -50px;
    opacity: 1
  }
  100% {
    left: 150px;
    top: -50px;
    opacity: 0
  }
}
<div id="container">
  <div class="wrapper">
      <div class="box box1"></div>
  </div>
  <div class="wrapper">
      <div class="box box2"></div>
  </div>
    <div class="meet">
    </div>
</div>