CSS 实现小球的曲线运动

本文介绍了两种使用CSS实现小球曲线运动的方法。第一种是结合animation动画,通过cubic-bezier贝塞尔曲线控制小球的速度变化,实现向右上方的曲线运动。第二种方法利用left和top绝对定位,配合transition属性改变小球的位置,实现简单的平滑移动。此外,还讲解了贝塞尔曲线的概念及其在动画速度控制中的应用。
摘要由CSDN通过智能技术生成

CSS 实现小球的曲线运动

方法一:使用 animation 动画

不同的两个方向的 animation 结合起来,就可以实现曲线运动的功能

向右的 animation + 向上的 animation = 向右向上的曲线运动

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

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        .ball {
            height: 60px;
            width: 60px;
            border-radius: 50%;
            position: absolute;
            /* 设置初始位置 */
            bottom: 10px;
            left: 10px;
            background: red;
        }

        .top_right {
            /* 同时设置两个动画 */
            animation: to-right 3s 0.4s infinite linear,
                to-top 3s 0.4s infinite cubic-bezier(0.15, 0.76, 0.25, 0.86);
            animation-fill-mode: forwards;
        }

        /* 向上运动 */
        @keyframes to-top {
            0% {
                bottom: 10px;
            }

            100% {
                bottom: 300px;
                background-color: yellow;
            }
        }

        /* 向右运动 */
        @keyframes to-right {
            0% {
                left: 10px;
                transform: scale(0.7);
            }

            100% {
                left: 300px;
                /* 小球缩小为0.3倍 */
                transform: scale(0.3);
                /* 小球的背景变为黄色 */
                background-color: yellow;
            }
        }
    </style>
</head>

<body>
    <div class="ball top_right color"></div>
</body>

</html>

效果:
在这里插入图片描述

关于贝塞尔曲线 (cubic-bezier)

贝塞尔曲线就是速度曲线

用来描述小球运动过程中的速度,比如先快后慢、先慢后快…

使用样式:transition: all 2s cubic-bezier(0.48, -0.53, 0.47, 1.44);

设置了这一属性的变化将会按照这一贝塞尔速度曲线来运动

像这样的都是贝塞尔曲线:

ease-in-out

ease

cubic-bezier(0.48, -0.53, 0.47, 1.44)

...

可以调试出想要的贝塞尔曲线:调制贝塞尔曲线的网址

具体的代码:

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

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>关于贝塞尔曲线</title>
    <style>
        .animation {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: rgb(243, 224, 14);
            transition: all 2s cubic-bezier(0.68, -0.56, 0.67, 1.65);
        }

        .animation:hover {
            transform: translateX(100px);
        }
    </style>
</head>

<body>
    <div class="animation"></div>
</body>

</html>

效果:
在这里插入图片描述

方法二:使用 left + top + 绝对定位

(建议使用动画)

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>昼短苦夜长,何不秉烛游</title>
    <style>
        .ball {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            /* 使用绝对定位 */
            position: absolute;
            top: 10px;
            left: 10px;
            background-color: red;
            /* 下面将transition分开来写 */
            transition-property: left, top, background-color;
            transition-duration: 2s, 2s, 2s;
            transition-timing-function: linear;
        }

        .ball:hover {
            top: 300px;
            left: 300px;
        }
    </style>
</head>

<body>
    <div class="ball"></div>
</body>

</html>

效果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

城南顾北

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值