函数封装实现页面动作功能

    技术2022-07-12  82

    // 用途:在页面上实现来回移动的盒子 // obj:要移动的对象元素 // target:移动距离 // callback:回调函数,跑完之后做什么操作 function move(obj, target, callback) { // 先清除,以免产生过多的定时器 clearInterval(obj.timer) obj.timer = setInterval(function () { //step后面的公式为(目标位置-当前位置)/10 作为缓慢移动的公式 var step = (target - obj.offsetLeft) / 10 // 如果是正着走就往上取整,如果倒着走就是地板取整。能得出比较精确的数 step = step > 0 ? Math.ceil(step) : Math.floor(step) obj.style.left = obj.offsetLeft + step + 'px' if (obj.offsetLeft == target) { clearInterval(obj.timer) if (callback) { callback() //这里是回调函数 } } }, 10); }
    Processed: 0.031, SQL: 9