CSS动画 – 位置到原始位置

前端之家收集整理的这篇文章主要介绍了CSS动画 – 位置到原始位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我今天想玩css动画.

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

所以我使用了变换和动画属性.

这是我写的代码到现在为止.

  1. $(".circle").click(function(){
  2. if($(this).hasClass('centerOfPage')){
  3. $(this).removeClass('centerOfPage');
  4. }else{
  5. $(this).addClass('centerOfPage');
  6. }
  7. });
  1. .circle{
  2. width: 100px;
  3. height: 100px;
  4. border-radius: 50%;
  5. margin: 10px;
  6. }
  7. .one{
  8. background-color: red;
  9. }
  10. .two{
  11. background-color: blue;
  12. }
  13. .three{
  14. background-color: yellow;
  15. }
  16. .four{
  17. background-color: green;
  18. }
  19.  
  20. .centerOfPage{
  21. position: fixed;
  22. top: 50%;
  23. left: 50%;
  24. width: 100%;
  25. height: 100%;
  26. border-radius: 5%;
  27. -webkit-transform: translate(-50%,-50%);
  28. transform: translate(-50%,-50%);
  29. animation : centerOfPageAnimate 3s;
  30. }
  31. @keyframes centerOfPageAnimate {
  32. 0% {
  33. top: 0%;
  34. left: 0%;
  35. width: 100px;
  36. height: 100px;
  37. border-radius: 50%;
  38. transform: translate(0,0);
  39. }
  40. 100% {
  41. top: 50%;
  42. left: 50%;
  43. -webkit-transform: translate(-50%,-50%);
  44. width: 100%;
  45. height: 100%;
  46. border-radius: 5%;
  47. }
  48. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  2. <div class="container">
  3. <div class="circle one"></div>
  4. <div class="circle two"></div>
  5. <div class="circle three"></div>
  6. <div class="circle four"></div>
  7. </div>

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

>当您点击任何圆圈时,他们的动画将从顶角开始,而不是从它们所在的位置开始.
>当你再次点击div然后他们回到他们的位置,但它没有动画,我再次希望从其他形状的动画圈到他们相同的位置.

这是codepen相同.
谢谢.

@H_301_22@

解决方法

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

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

请参阅jQuery Snippet和Codepen

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

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

@H_301_22@ @H_301_22@

猜你在找的CSS相关文章