【发布时间】: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时,它不会“从它们所在的位置开始”……
标签: css css-transitions css-animations