运动
运动主要是动画的操作,主要是操作某个document元素的属性变化(位置变化)
运动主要的三步骤
-
使用定时器来定时更改对应的内容
-
实时获取对应的元素的属性及相关内容
-
判断是否到达目标位置(到达后清除定时器)
匀速运动
概述:匀速运动的本质就是每次变化值都是同一个
示例
缓冲运动
概述: 缓冲运动的本质就是每次变化的值越来越小
示例
//缓冲运动
//获取div和对应的按钮
var btns = document.querySelectorAll('button')
var box = document.querySelector('div')
btns[0].onclick = ()=>{
//获取当前位置
var current = parseInt(getStyle(box,'left'))
//定义步长和对应的目标位置
var step = 10 //初始步长
var target = 500 //目标位置
//定时器
var timer = setInterval(() => {
//控制步长的变化 离目标会越来越小 把步长和这个距离绑定
step = (target - current)/20 > 0 ? Math.ceil((target - current)/20) : Math.floor((target - current)/20)
//控制当前位置的变化
current += step
//给当前位置赋值
box.style.left = current + 'px'
//到达目标位置清除定时器
if(current == target){
clearInterval(timer)
}
}, 80);
}
匀速移动和缓冲移动的封装
//获取样式的方法
function getStyle(ele, attr) {
return window.getComputedStyle ? window.getComputedStyle(ele, null)[attr] : ele.currentStyle[attr]
}
//缓冲动画为true 不传就是匀速
function move(ele, target, isBuffer) {
//获取当前位置
var current = parseInt(getStyle(ele, 'left'))
//定义步长和对应的目标位置
var step = target - current > 0 ? 10 : -10
//定时器
var timer = setInterval(() => {
if (isBuffer) {
//控制步长的变化 离目标会越来越小 把步长和这个距离绑定
step = (target - current) / 20 > 0 ? Math.ceil((target - current) / 20) : Math.floor((target - current) / 20)
}
//控制当前位置的变化
current += step
//给当前位置赋值
ele.style.left = current + 'px'
//到达目标位置清除定时器
if (current == target) {
clearInterval(timer)
}
}, 20)
}
透明度变化
//透明度变化
function opacityAction(box,target){
//获取当前的透明度
//设置步长及对应的目标位置
var step
var current = parseFloat(getStyle(box, 'opacity'))
//开启定时器
var timer = setInterval(() => {
// step = target-current>0?0.1:-0.1
//当透明度到达目标位置清除定时器
if(Math.abs(target-current) <= step){
clearInterval(timer)
}
// 最小值 0.01
step = (target-current)*100/10 > 0 ? Math.ceil((target-current)*100/10)/100 : Math.floor((target-current)*100/10)/100
//控制透明度的变化
current += step
box.style.opacity = current
}, 20);
}
宽度变化
//获取按钮及div
var box = document.querySelector('div')
var btn = document.querySelector('button')
btn.onclick = ()=>{
//获取开始的宽度 以及步长
var current = parseFloat(getStyle(box,'width'))
var step
var target = 500
//设置目标位置
var timer = setInterval(() => {
//判断是否到达目标位置
if(Math.abs(target-current) <= step){
clearInterval(timer)
}
//设置步长
// step = target - current > 0 ? 10 : -10
step = (target-current )/10 > 0 ? Math.ceil((target-current)/10) : Math.floor((target-current)/10)
//设置当前的位置
current += step
box.style.width = current + 'px'
}, 20);
}
//获取样式的方法
function getStyle(ele, attr) {
return window.getComputedStyle ? window.getComputedStyle(ele, null)[attr] : ele.currentStyle[attr]
}
简易封装move.js
//获取样式的方法
function getStyle(ele, attr) {
return window.getComputedStyle ? window.getComputedStyle(ele, null)[attr] : ele.currentStyle[attr]
}
//缓冲动画为true 不传就是匀速
function move(ele, target, isBuffer = false) {
clearInterval(ele.timer) //清除之前的定时器影响
//针对于px为单位的 width,height,left,top
//opacity不需要px
//zIndex不需要动画
//获取target对象里面的所有的key
ele.timer = setInterval(() => {
var flag = true
for (let key in target) {
//获取当前位置
var current = parseFloat(getStyle(ele, key)) ? parseFloat(getStyle(ele, key)) : 0
//定义步长和对应的目标位置
if(key == 'opacity'){
var step = target[key] - current > 0 ? 0.01 : -0.01
}else{
var step = target[key] - current > 0 ? 10 : -10
}
//定时器
if (key == 'zIndex') { //层级
ele.style[key] = target[key] //直接设置
} else {
//没有到达设置为false
if (Math.abs(target[key] - current ) > step) {
flag = false
}
if (isBuffer) { //如果是缓冲的
if (key == 'opacity') { //透明度
// 最小值 0.01
step = (target[key] - current) * 100 / 10 > 0 ? Math.ceil((target[key] - current) * 100 / 10) / 100 : Math.floor((target[key] - current) * 100 / 10) / 100
} else { //其他的
//控制步长的变化 离目标会越来越小 把步长和这个距离绑定
step = (target[key] - current) / 10 > 0 ? Math.ceil((target[key] - current) / 10) : Math.floor((target[key] - current) / 10)
}
}
//控制当前位置的变化
current += step
//给当前位置赋值
if (
