JS运动总结封装

JS运动总结封装

1.匀速运动,往返运动

//第一个参数为需要移动的元素,第二个为速度,
//第三个为目标,第四个前进按钮,第五个后退按钮

function constantBack(node, speed, target, node1, node2) {
    let time = null;
    let time1 = null
    node1.addEventListener("click", function() {
        clearInterval(time)
        clearInterval(time1)
        time = setInterval(() => {
            node.style.left = node.offsetLeft + speed + "px"
            if (node.offsetLeft > target) {
                node.style.left = target + "px"
                clearInterval(time)
            }
        }, 50);
    })
    node2.addEventListener("click", function() {
        clearInterval(time)
        clearInterval(time1)
        time1 = setInterval(() => {
            node.style.left = node.offsetLeft - speed + "px"
            if (node.offsetLeft < 0) {
                node.style.left = 0 + "px"
                clearInterval(time1)
            }
        }, 50);
    })
}

2. 匀速透明运动

// 匀速透明运动
function linerOpac(node, speed) {
    let time = null
    let time1 = null
    node.onmouseover = function() {
        clearInterval(time1)
        clearInterval(time)
        time = setInterval(() => {
            node.style.opacity = Number(getComputedStyle(node, false)["opacity"]) - speed
            if (node.style.opacity < 0) {
                node.style.opacity = 0
                clearInterval(time)
            }
        }, 50);

    }
    node.onmouseout = function() {
        clearInterval(time)
        clearInterval(time1)
        time1 = setInterval(() => {
            node.style.opacity = Number(getComputedStyle(node, false)["opacity"]) + speed
            if (node.style.opacity > 1) {
                node.style.opacity = 1
                clearInterval(time1)
            }
        }, 50);
    }
}

3. 缓冲运动

//缓冲运动
function bufferRun(node, target, node1, node2) {
    let time = null;
    let time1 = null
    node1.addEventListener("click", function() {
        clearInterval(time)
        clearInterval(time1)
        time = setInterval(() => {
            node.style.left = node.offsetLeft + Math.ceil((target - node.offsetLeft) / 10) + "px"
            if (node.offsetLeft > target) {
                node.style.left = target + "px"
                clearInterval(time)
            }
        }, 50);
    })
    node2.addEventListener("click", function() {
        clearInterval(time)
        clearInterval(time1)
        time1 = setInterval(() => {
            node.style.left = node.offsetLeft - Math.ceil((node.offsetLeft) / 10) + "px"
            if (node.offsetLeft < 0) {
                node.style.left = 0 + "px"
                clearInterval(time1)
            }
        }, 50);
    })
}

4. 反弹运动

// 反弹运动
function rebound(node, leftMax, topMax) {
    let speedL = Math.round(Math.random() * 5 + 5)
    let speedT = Math.round(Math.random() * 5 + 5)
    time = setInterval(() => {
        console.log(leftMax, topMax);
        node.style.left = node.offsetLeft + speedL + "px"
        node.style.top = node.offsetTop + speedT + "px"
        if (node.offsetLeft >= leftMax || node.offsetLeft < 0) {
            speedL = -speedL
        }
        if (node.offsetTop >= topMax || node.offsetTop < 0) {
            speedT = -speedT
            console.log(speedT);
        }
    }, 50);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值