抛物线运动可实现方式
1.JavaScript实现(运用抛物线函数:a*x*x+b*x+c)
2.Css分层动画实现
本文主要介绍如何运用css实现抛物线运动
* Js的实现方式可参考张鑫旭的分享:http://www.zhangxinxu.com/wordpress/?p=3855
疑惑点
Css的animation(动画)和transition(变换)一般只能实现从A点到B点之间的直线运动,无论无何调整贝塞尔曲线(bezier curve)都无法使其进行曲线运动,只可调整运动的弹性速度,沿着X轴与Y轴移动的值永远相同。
解决方案
当然我们可以换一个思路:一个平面运动的物体永远是受X轴以及Y轴的力控制,既然单独对一个物体无法进行改变,不如再增加一个元素,使其互相影响,一方负责X轴运动,另一方负责Y轴运动。
既然要使其互相影响,那就使其中一个元素镶嵌在另一个元素之中,并调整双方的运动曲线或运动速度,不使其同步。
使外层容器控制 X 轴运动,内层球形控制 Y 轴运动。X 轴使用ease-in,Y 轴使用ease-out 。
为了清晰地看到两个元素沿着弧形路径是如何相互作用的,给外层容器元素加个边框
当然,与其使用两个元素来实现,不如运用伪元素,方法类似。
贝塞尔曲线理解:http://www.html-js.com/article/1628
贝塞尔曲线工具:http://cubic-bezier.com/
贝塞尔曲线绘制:http://myst729.github.io/bezier-curve/
代码部分:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
.item {
width: 200px;
height: 200px;
position: relative;
border: 2px solid #dddddd;
border-width: 2px 2px 0 0;
margin-left: 100px;
margin-top: 100px;
background: linear-gradient(to top, #ddd 10%, transparent 10%), linear-gradient(to right, #ddd 10%, transparent 10%);
background-size: 100% 20%, 20%, 100%;
}
.qiu {
width: 20px;
height: 20px;
background-color: blue;
border-radius: 50%;
position: absolute;
transition: all 0.8s;
}
.con {
width: 20px;
height: 20px;
background-color: transparent;
border-radius: 50%;
position: absolute;
top: -10px;
left: -10px;
transition: all 0.8s;
}
.item:hover .con {
transform: translateX(200px);
transition-timing-function: linear;
}
.item:hover .qiu {
transform: translateY(200px);
transition-timing-function: cubic-bezier(.58, -0.42, 1, .65)
}
</style>
</head>
<body>
<div class="item">
<i class="con"> <i class="qiu"> </i> </i>
</div>
</body>
</html>