CSS   发布时间:2022-04-17  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了CSS动画 – 位置到原始位置大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我今天想玩css动画.

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

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

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

$(".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%);
    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相同.
谢谢.

解决方法

由于您已经在使用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,borderRadius:'5%',height:'100%',width:'100%'});
 }
});

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

大佬总结

以上是大佬教程为你收集整理的CSS动画 – 位置到原始位置全部内容,希望文章能够帮你解决CSS动画 – 位置到原始位置所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。