/**
* 简单版多属性运动函数
* @param { ELEMENT } ele 要运动的元素
* @param { OBJECT } target 要运动的属性(对象)
* @param { FUNCTION } fn 运动结束的回调函数
*/
function move(ele, target, fn = () => {}) {
let count = 0
for (let key in target) {
if (key === 'opacity')
target[key] *= 100
count++
let timer = setInterval(() => {
let current = key === 'opacity' ? getStyle(ele, 'opacity') * 100 : parseInt(getStyle(ele, key))
let distance = (target[key] - current) / 10
distance = distance > 0 ? Math.ceil(distance) : Math.floor(distance)
if (current === target[key]) {
clearInterval(timer)
count--
if (!count)
fn()
} else {
ele.style[key] = key === 'opacity' ? (current + distance) / 100 : current + distance + 'px'
}
}, 30)
}
}
简单版多属性运动函数-笔记
于 2022-08-10 21:30:58 首次发布