1、匀速运动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 200px;
height: 200px;
background-color: saddlebrown;
position: relative;
left: -200px;
}
.btn {
width: 60px;
height: 40px;
display: flex;
justify-content: center;
align-items: center;
background-color: lightblue;
position: absolute;
top: 50%;
right: -60px;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="box">
<span class="btn">动画</span>
</div>
<script>
window.onload = function () {
var box = document.querySelector('.box')
var timer = null
var speed = 0
box.onmouseover = function () {
move(this,0)
}
box.onmouseout = function () {
move(this,-200)
}
function move(obj,end) {
clearInterval(timer)
speed = end > obj.offsetLeft ? 5:-5
timer = setInterval(() => {
if (obj.offsetLeft === end) {
return clearInterval(timer)
}
obj.style.left = obj.offsetLeft + speed + 'px'
}, 20)
}
}
</script>
</body>
</html>

2、缓动运动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 200px;
height: 200px;
background-color: saddlebrown;
position: relative;
left: -200px;
}
.btn {
width: 60px;
height: 40px;
display: flex;
justify-content: center;
align-items: center;
background-color: lightblue;
position: absolute;
top: 50%;
right: -60px;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="box">
<span class="btn">动画</span>
</div>
<script>
window.onload = function () {
var box = document.querySelector('.box')
var btn = document.querySelector('.btn')
var timer = null
/*
加速度speed = (结束状态 - 初始状态) / 系数
*/
var speed = 0
// 鼠标进入元素时候元素的最终状态值
var end1 = 0
// 鼠标移除元素时候元素的最终状态值
var end2 = -200
box.onmouseover = function () {
move(this, end1)
}
box.onmouseout = function () {
move(this, end2)
}
function move(obj, end) {
clearInterval(timer)
timer = setInterval(() => {
if (obj.offsetLeft === end) {
return clearInterval(timer)
}
speed = (end - obj.offsetLeft) > 0 ? speed = Math.ceil((end1 - obj.offsetLeft) / 20) : speed = Math.floor((end2 - obj.offsetLeft) / 20)
obj.style.left = obj.offsetLeft + speed + 'px'
}, 30)
}
}
</script>
</body>
</html>

3、透明度动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 200px;
height: 200px;
background-color: brown;
opacity: 0.3;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
window.onload = function() {
var box = document.querySelector('.box')
var timer = null,speed=0,alpha = 30
box.onmouseover = function() {
opacityAnimation(this,100)
}
box.onmouseout = function() {
opacityAnimation(this,30)
}
function opacityAnimation(obj,end) {
clearInterval(timer)
timer = setInterval(()=>{
if(alpha === end) return clearInterval(timer)
speed = end > alpha ? 10 : -10
alpha += speed
obj.style.opacity = alpha / 100
},50)
}
}
</script>
</body>
</html>

4、多物体缓动运动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div {
width: 300px;
height: 150px;
background-color: chartreuse;
margin: 15px 0;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<script>
window.onload = function() {
var oDivs = document.querySelectorAll('div')
for(let i=0;i<oDivs.length;i++) {
oDivs[i].onmouseover = function() {
changeWidth(this,600)
}
oDivs[i].onmouseout = function() {
changeWidth(this,300)
}
}
var speed = 0;
function changeWidth(obj,end) {
// 对于多物体,定时器的返回值要绑定到当前对象里去
clearInterval(obj.timer)
obj.timer = setInterval(()=>{
let oWidth = parseInt(getStyle(obj,'width'))
if(end === oWidth) return clearInterval(obj.timer)
speed = (end - oWidth) > 0 ? Math.ceil((end - oWidth) / 20) : Math.floor((end - oWidth) / 20)
obj.style.width = `${oWidth + speed}px`
},20)
}
/**
* 获取元素属性的值
* obj: 当前元素对象
* attr: 当前元素对象属性
*/
function getStyle(obj, attr) {
if (obj.currentStyle) {
// 兼容ie
return obj.currentStyle[attr];
}else {
// 主流浏览器
return getComputedStyle(obj, null)[attr]
}
}
}
</script>
</body>
</html>

5、封装简单的动画框架
var speed = 0;
/**
* 动画的函数
* @param {Object} obj 当前的对象
* @param {Object} attr 当前元素对象的属性
* @param {Object} endTarget 末尾位置
*/
function startAnimation(obj, json, fn) {
// 针对于多物体运动,定时器的返回值要绑定当前的对象中.
clearInterval(obj.timer);
obj.timer = setInterval(function() {
var cur = 0;
var flag = true; //标杆 如果true,证明所有的属性都到达终点值
for (var attr in json) {
// 0 获取样式属性
// 透明度变化处理
switch (attr) {
case 'opacity':
cur = Math.round(parseFloat(getStyle(obj, attr)) * 100);
break;
case 'scrollTop':
cur = obj[attr]
break;
default:
cur = parseInt(getStyle(obj, attr));
break;
}
// 1.求速度
speed = (json[attr] - cur) / 10;
speed = json[attr] > cur ? Math.ceil(speed) : Math.floor(speed);
// 2.临界处理
if (json[attr] !== cur) {
flag = false;
}
// 3.运动起来
switch (attr) {
case 'opacity':
obj.style[attr] = `alpha(opacity: ${cur + speed})`;
obj.style[attr] = (cur + speed) / 100;
break;
case 'scrollTop':
obj.scrollTop = cur + speed;
default:
obj.style[attr] = cur + speed + 'px';
break;
}
}
if (flag) {
clearInterval(obj.timer);
if (fn) {
fn();
}
return;
}
}, 30);
}
/**
* 获取元素属性的函数
* @param {Object} obj 当前元素对象
* @param {Object} attr 当前元素对象的属性
*/
function getStyle(obj, attr) {
if (obj.currentStyle) {
// 兼容ie
return obj.currentStyle[attr];
} else {
// 兼容主流浏览器
return getComputedStyle(obj, null)[attr];
}
}