js部分
var timer = null;
var div = document.getElementsByTagName("div");
function getStyle(obj, style) {
if(window.getComputedStyle) {
return window.getComputedStyle(obj,null)[style];
}else {
return obj.currentStyle[style];
}
}
var targetObj = {
width: 400,
height: 400,
opacity: 30,
top: 400,
left: 400
}
div[0].onmouseenter = function() {
animate(this, targetObj, 'easeOut', 500, function() {
animate(div[1], targetObj, 'easeOut', 500)
});
}
function now() {
return (new Date()).getTime();
}
// 运动对象、目标位置大小、运动函数、运动时间、回调
function animate(obj, json, fx, times, callback) {
var iCur = {};
for(var attr in json) {
if(attr == 'opacity') {
iCur[attr] = Math.ceil(getStyle(obj,attr) * 100);
}else {
iCur[attr] = parseInt(getStyle(obj,attr));
}
}
var startTime = now();
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var changeTime = now();
var t = times - Math.max(0,startTime - changeTime + times);
for(var attr in json) {
var value = Tween[fx](t,iCur[attr],json[attr] - iCur[attr],times);
if(attr == 'opacity') {
obj.style.opacity = value / 100;
}else {
obj.style[attr] = value + 'px';
}
}
if(t == times) {
console.log(1)
clearInterval(obj.timer);
typeof callback == 'function' ? callback() : '';
}
},16)
}
// function startMove(obj, json, fx, times, callback) {
// clearInterval(obj.timer);
// var speed , cur;
// obj.timer = setInterval(function(){
// var stop = true;
// for(var prop in json) {
// if(prop == "opacity") {
// cur = parseFloat( getStyle(obj, prop) ) * 100;
// }else {
// cur = parseInt( getStyle(obj, prop) );
// }
// speed = (json[prop] - cur) / 7;
// speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
// if(prop == "opacity") {
// obj.style.opacity = (cur + speed) / 100;
// }else {
// obj.style[prop] = (cur + speed) + "px";
// }
// if(cur != json[prop]) {
// stop = false;
// }
// }
// if(stop) {
// clearInterval(obj.timer);
// typeof callback == "function" ? callback() : "";
// }
// },20)
// }
// div[0].onmouseenter = function() {
// startMove(this, targetObj, function() {
// startMove(div[1], targetObj)
// });
// }
var Tween = {
linear : function(t,b,c,d) { //匀速
return c * t / d + b;
},
easeIn : function(t,b,c,d) { //加速
return c * (t /= d) * t + b;
},
easeOut : function(t,b,c,d) { //减速
return -c * (t /= d) * (t - 2) + b;
},
easeBoth : function(t,b,c,d) { //加速减速
if((t /= d / 2) < 1) {
return c / 2 * t * t + b;
}
return -c / 2 * ((--t) * (t - 2) - 1) + b;
},
easeInStrong : function(t,b,c,d) { //加加速
return c * (t /= d) * t * t * t + b;
}
}
html、css部分
<!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>
* {
margin: 0;
padding: 0;
}
/* div {
width: 100px;
height: 100px;
background-color: orange;
/* position: absolute; */
/* margin-bottom: 50px;
border: 1px solid #000; */
/* top: 0;
left: 0; */
/* } */
.top {
width: 100px;
height: 100px;
opacity: 1;
background: orangered;
position: absolute;
top: 0;
left: 0;
}
.bottom {
position: absolute;
width: 100px;
height: 100px;
background: orangered;
opacity: 1;
top: 300px;
left: 0;
}
</style>
</head>
<body>
<!-- <div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div> -->
<div class="top"></div>
<div class="bottom"></div>
</body>
</html>