very easy 的抛物线动画

305 阅读1分钟

抛前

抛过程中


抛完


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>抛物线动画</title>
    <link rel="stylesheet" href="parabola.css" />    
<style>
.animation-box {
    width: 80%;
    margin: 20px auto;
}
.top-box,
.bottom-box {
    display: flex;
    justify-content: space-between;
}
.top-box>div,
.bottom-box>div {
    width: 20px;
    height: 20px;
    border-radius: 10px;
    background: plum;
    position: relative;
    transition: left 1s linear, top 1s ease-in;
}
.center-box {
    width: 100%;
    height: 500px;
    display: flex;
    justify-content: center;
    align-items: center;
}
.center-middle-box {
    width: 20px;
    height: 20px;
    border-radius: 10px;
    background: pink;
}
</style>
</head>

<body>
    <div class="animation-box">
        <div class="top-box">
            <div class="left-top-box" id='left-top-box-id' onclick="startAnimation('left-top-box-id')"></div>
            <div class="right-top-box" id="right-top-box-id" onclick="startAnimation('right-top-box-id')"></div>
        </div>
        <div class="center-box">
            <div class="center-middle-box" id="center-middle-box-id"> </div>
        </div>
        <div class="bottom-box">
            <div class="left-bottom-box" id="left-bottom-box-id" onclick="startAnimation('left-bottom-box-id')"></div>
            <div class="right-bottom-box" id="right-bottom-box-id" onclick="startAnimation('right-bottom-box-id')">
            </div>
        </div>
    </div>
</body>
<script>

    function startAnimation(startId, endId = 'center-middle-box-id', xPoint = 'left', yPoint = 'top') {
        const start = document.getElementById(startId)
        const end = document.getElementById(endId)
        const startx = start.offsetLeft
        const starty = start.offsetTop
        const endx = end.offsetLeft
        const endy = end.offsetTop
        const x = endx - startx
        const y = endy - starty
        start.style.top = '0px';
        start.style.left = '0px';
        start.style.background = 'aquamarine'
        start.style.transition = `${xPoint} 0s, ${yPoint} 0s `;
        setTimeout(() => {
            start.style.top = y + 'px';
            start.style.left = x + 'px';
            start.style.transition = `${xPoint} 1s linear, ${yPoint} 1s ease-in`;
        }, 10);
    }


</script>

</html>