var speed = (iTarget-oDiv.offsetLeft)/20;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
缓冲动画
Math.floor() 向下取整;
Math.ceil() 向上取整;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cartoon</title>
<style type="text/css">
body, div, span {
margin: 0;
padding: 0;
}
#div1 {
width: 200px;
height: 200px;
background: red;
position: relative;
left: -200px;
top: 0;
}
#div1 span {
width: 20px;
height: 50px;
background: blue;
position: absolute;
left: 200px;
top: 75px;
}
</style>
</head>
<body>
<script>
window.onload = function() {
var oDiv = document.getElementById("div1");
oDiv.onmouseover = function() {
move(0);
}
oDiv.onmouseout = function() {
move(-200);
}
}
var timer = null;
function move(dis) {
clearInterval(timer);
var oDiv = document.getElementById("div1");
timer = setInterval(function() {
var speed = (dis - oDiv.offsetLeft)/20;
speed = speed >0?Math.ceil(speed):Math.floor(speed);
if(oDiv.offsetLeft == dis) {
clearInterval(timer);
}
else {
oDiv.style.left = oDiv.offsetLeft + speed + 'px';
}
},30)
}
</script>
<div id="div1">
<span id="share">分享</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>缓冲运动</title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
#div1{
width: 200px;
height: 200px;
background-color: red;
position: relative;
left: -200px;
top: 0;
}
#div1 span{
width: 20px;
height: 50px;
background-color: blue;
position:absolute;
left: 200px;
top: 75px;
}
</style>
<script type="text/javascript">
window.onload=function(){
var oDiv=document.getElementById("div1");
//鼠标移入事件
oDiv.onmouseover=function(){
starMove(0);
}
//鼠标移出事件
oDiv.onmouseout=function(){
starMove(-200);
}
var timer = null;
function starMove(target){
clearInterval(timer);
var oDiv=document.getElementById("div1");
//创建一个定时器timer
timer=setInterval(function(){
var speed =(target - oDiv.offsetLeft)/20;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
if(oDiv.offsetLeft==target){
clearIntral(timer);
}else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30);
}
}
</script>
</head>
<body>
<div id = div1>
<span>分享</span>
</div>
</body>
</html>
Math.floor(num)向下取整
Math.ceil(num)向上取整
0.5向上取整为1,向下取整为0
-0.5向上取整为0,向下取整为-1
offsetLeft会越来越大,与目标差值越来越下,speed越来越小。
https://github.com/cbat01/js-Animation-effect
做运动时要做向上与向下取整
speed = speed >0? Math.ceil(speed):Math.floor(speed);
缓冲运动的关键在于,速度随当前状态与目标状态之间的差值呈线性关系,但是速度并不是线性的,而是一个曲线(类比火车停靠,火车启动)
对于在JS内修改了CSS的数值,一定要对数值进行一个处理(CSS的属性value不支持小数点)。
speed = speed>0?Math.ceil(speed):Math.floor(speed);
Math.floor(num);向下取整
Math.ceil(num);向上取整
var speed = (iTarget-ele.offsetLeft)/20;
speed = speed>0?Math.ceil(speed):Math.floor(speed);//凡是运动,要添加向下向上取整,以防出现错误
ele.style.left = ele.offsetLeft + speed + 'px';
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>缓冲运动</title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
#div1{
width: 200px;
height: 200px;
background-color: red;
position: relative;
left: -200px;
top: 0;
}
#div1 span{
width: 20px;
height: 50px;
background-color: blue;
position:absolute;
left: 200px;
top: 75px;
}
</style>
<script type="text/javascript">
window.onload=function(){
var oDiv=document.getElementById("div1");//oDiv是局部变量,只在此函数内有效
//鼠标移入事件
oDiv.onmouseover=function(){
starMove(0);
}
//鼠标移出事件
oDiv.onmouseout=function(){
starMove(-200);
}
var timer = null;
function starMove(target){
clearInterval(timer);//解决“speed”的叠加问题
var oDiv=document.getElementById("div1");
//创建一个定时器timer
timer=setInterval(function(){
var speed =(target - oDiv.offsetLeft)/10;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
if(oDiv.offsetLeft==target){
clearIntral(timer);//符合条件运动停止
}else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30);
}
}
</script>
</head>
<body>
<div id = div1>
<span>分享</span>
</div>
</body>
</html>
var timer = null; function startMove(iTarget) { clearInterval(timer); timer = setInterval(function(){ var speed = 0; var oDiv = document.getElementById("oDiv"); speed = (iTarget-oDiv.offsetLeft)/10; speed = speed>0?Math.ceil(speed):Math.floor(speed); if (oDiv.offsetLeft!=iTarget){ oDiv.style.left = oDiv.offsetLeft + speed +"px"; } },30) } window.onload = function() { var oDiv = document.getElementById("oDiv"); oDiv.onmouseover = function() { startMove(0); } oDiv.onmouseout = function () { startMove(-200); } }
iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);//速度如果没有取整,会导致无法到达目标,一则占用资源