【问题标题】:CSS animation - position to original placeCSS动画 - 位置到原始位置
【发布时间】:2015-12-24 02:00:17
【问题描述】:

我今天想玩css动画。

所以我的基本想法是创建四个圆圈,然后当用户点击那个圆圈时,它应该转到页面中心,然后它应该变成其他形状。

所以我用过,transform和animation属性。

这是我写到现在的代码。

$(".circle").click(function(){
  if($(this).hasClass('centerOfPage')){
    $(this).removeClass('centerOfPage');
  }else{
    $(this).addClass('centerOfPage');
  }
});
.circle{
  width: 100px;
  height: 100px;
  border-radius: 50%;
  margin: 10px;
}
.one{
  background-color: red;
}
.two{
  background-color: blue;
}
.three{
  background-color: yellow;
}
.four{
  background-color: green;
}

 .centerOfPage{
    position: fixed;
    top: 50%;
    left: 50%;
    width: 100%;
    height: 100%;
    border-radius: 5%;
   -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    animation : centerOfPageAnimate 3s;
   
}
@keyframes centerOfPageAnimate {
  0% {
    top: 0%;
    left: 0%;
    width: 100px;
    height: 100px;
    border-radius: 50%;
    transform: translate(0, 0);    
  }
  100% {
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    width: 100%;
    height: 100%;
    border-radius: 5%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
  <div class="circle one"></div>
  <div class="circle two"></div>
  <div class="circle three"></div>
  <div class="circle four"></div>
</div>

现在这里有一些你会注意到的问题..

  • 当您单击任何圆圈时,它们的动画将从上角开始,而不是从它们所在的位置开始。
  • 当您再次单击 div 时,它们将返回到它们的位置但它没有动画,我再次希望该动画从其他形状变为圆形并移动到它们的相同位置。

这里是codepen 相同。 谢谢。

【问题讨论】:

  • 当然,当您将之前根本没有定位的元素切换到position:fixed时,它不会“从它们所在的位置开始”……
  • @nitz:GSAP 可以对您的动画执行以下操作:Codepen。这只是一个快速演示,这里有很多可以改进的地方。但它应该让您对 GSAP 有所了解。我知道这不是您想要的,但我想如果您还不知道,我应该向您介绍它。

标签: css css-transitions css-animations


【解决方案1】:

由于您已经在使用 jQuery,我决定 100% 使用它。我的代码和你的代码之间的主要区别在于我在加载时存储每个圆圈的位置,并且我不依赖 CSS3 关键帧。

我使用 jQuery .data 方法在加载时存储圆的位置。现在,当您删除“centerOfPage”类时,您可以使用 jQuery 恢复到以前存储的圆圈位置。

查看 jQuery Snippet 和 Codepen

$('.circle').each(function () {
  $(this).data('circlePosTop', $(this).position().top);
});
		
$(".circle").click(function(){
 if($(this).hasClass('centerOfPage')){
   $(this).animate({top:$(this).data('circlePosTop'),left:0,borderRadius:'50%',height:'100px',width:'100px'}).removeClass('centerOfPage');
 } else {
   $(this).addClass('centerOfPage').animate({top:0,left:0,borderRadius:'5%',height:'100%',width:'100%'});
 }
});

http://codepen.io/anon/pen/OyWVyP

【讨论】:

  • 我强烈反对。 JQuery 非常繁重,应该尽量少用这种类型的视觉变化。
  • @SofiaFerreira,“非常重”在什么意义上? jQuery(更具体地说,JavaScript)动画早在 CSS 动画可用之前就已经存在了。今天的硬件比“当时”的硬件好,所以你可以争辩说今天它不像过去那样令人担忧。基于 JavaScript 的动画的唯一缺点是它们(通常)不利用硬件加速,这是您可以从基于 CSS 的动画中获得的优化。
【解决方案2】:

@nitz JQuery 非常繁重,应该尽可能少地使用它来进行这种类型的视觉更改。你做对了。

需要通过关键帧来移除动画,比较复杂。

请试试这个:

$(".circle").click(function(){
  $(this).toggleClass("centerOfPage");
});
.circle{
  width: 100px;
  height: 100px;
  border-radius: 50%;
  position: fixed;
  left: 30px;
  
  -moz-transition: all 0.5s ease .2s;
  -o-transition: all 0.5s ease .2s;
  -webkit-transition: all 0.5s ease .2s;
  transition: all 0.5s ease .2s; 
}
.one{
  background-color: red;
  top: 10px;
}
.two{
  background-color: blue;
  top: 120px;
}
.three{
  background-color: yellow;
  top: 230px;
}
.four{
  background-color: green;
  top: 340px;
}

 .centerOfPage{
   width: 100%;
   height: 100%;
   top: 0; bottom: 0;
   left: 0; right: 0;
   border-radius: 0; 
   z-index: 2;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="circle one"></div>
  <div class="circle two"></div>
  <div class="circle three"></div>
  <div class="circle four"></div>
</div>

【讨论】:

  • 如果您恳求不要将 jquery 用于此类应用程序,在我看来将 javascript 部分设为纯 javascript 似乎是合乎逻辑的,不是吗?毕竟只是一个小代码
【解决方案3】:

这可能是你要找的吗?

centerOfPage 类现在只用于启动动画,没有额外的定位,并且在关键帧声明中使用 % 我们可以先将其移动到页面的中心,然后使其变大(您也可以在步骤之间添加,如果你喜欢)

$(".circle").click(function(){
  $(this).toggleClass('centerOfPage'); //toggleClass saves you the if else
 });
.circle{
  width: 100px;
  height: 100px;
  border-radius: 50%;
  margin: 10px;
  transition: all 2s linear;  /* added transition to animate going back to start state*/
}
.one{
  background-color: red;
    
}
.two{
  background-color: blue;
}
.three{
  background-color: yellow;
}
.four{
  background-color: green;
}

 .centerOfPage{
   
    animation: test 2s forwards; /* add "forwards" here to stop the animation at end-frame */
  }


  @keyframes test{
   /* You can use percent to target steps during your animation:
      here it moves to the center at 10% animation duration and gets bigger    at 100 % animation duration 
    */
  10%{
  transform: translate(50%);
  }
  100%{
  width: 100vw;
  height: 100vh;
   border-radius: 5%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
  <div class="circle one"></div>
  <div class="circle two"></div>
  <div class="circle three"></div>
  <div class="circle four"></div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-07
    • 2011-04-20
    • 2014-06-14
    • 2019-06-06
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多