css3小球抛物线

css部分:

 <style>
        * {
            padding: 0;
            margin: 0;
        }
        html,body {
            width: 100%;
            height: 100%;
        }
        #ball {
            display: none;
            width:10px;
            height:10px;
            background: red;
            border-radius: 50%;
            position: fixed;
        }
    
</style>

html部分:

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

JavaScript部分

<script>
    var ball = document.getElementById('ball');
    document.onclick = function(e) {
        ball.style.top = e.pageY + 'px';
        ball.style.left = e.pageX + 'px';
        ball.style.transition = 'left 0s, top 0s';
        ball.style.display = 'block';
        setTimeout(function(){
            ball.style.top = window.innerHeight + 'px';
            ball.style.left = '500px';
            ball.style.transition = 'left 1s linear, top 1s cubic-bezier(.08,-0.35,.99,.33)';
        }, 20)
    }
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现小抛物线的过程可以分为两个部分:点击事件和抛物线动画。 首先,我们需要在 Vue3 中实现点击事件,并且获取点击位置的坐标,可以使用 `@click` 绑定点击事件,并且使用 `$event` 参数来获取点击位置的坐标。 ``` <template> <div class="container" @click="onClick"> <div class="ball" :style="{ top: ballTop + 'px', left: ballLeft + 'px' }"></div> </div> </template> <script> export default { data() { return { ballTop: 0, ballLeft: 0, }; }, methods: { onClick(event) { // 获取点击位置的坐标 const x = event.clientX; const y = event.clientY; // 实现抛物线动画 this.throwBall(x, y); }, throwBall(x, y) { // TODO: 实现抛物线动画 }, }, }; </script> <style scoped> .container { position: relative; width: 500px; height: 500px; border: 1px solid #ccc; } .ball { position: absolute; width: 50px; height: 50px; background-color: red; border-radius: 50%; } </style> ``` 接下来,我们需要在 `throwBall()` 方法中实现小抛物线动画。抛物线的公式为:$y = a x^2 + b x + c$,其中 $a$ 控制抛物线的弧度,$b$ 和 $c$ 控制抛物线的位置。 我们可以通过设置一个定时器,每隔一段时间更新小的位置,实现抛物线动画。在每次更新位置时,我们需要根据抛物线公式计算出小的新坐标。 ``` throwBall(x, y) { const startX = this.ballLeft; const startY = this.ballTop; const endX = x - 25; // 小的宽度是50px const endY = y - 25; // 小的高度是50px const a = 0.005; // 控制抛物线的弧度 const b = (endY - a * endX * endX) / endX; // 根据公式计算b const c = startY - a * startX * startX - b * startX; // 根据公式计算c let time = 0; const interval = setInterval(() => { time += 0.1; const ballLeft = startX + (endX - startX) * time; const ballTop = a * ballLeft * ballLeft + b * ballLeft + c; this.ballLeft = ballLeft; this.ballTop = ballTop; if (ballTop >= endY) { clearInterval(interval); } }, 10); }, ``` 最后,我们需要在 CSS 中设置小的初始位置和样式。 ``` .ball { position: absolute; width: 50px; height: 50px; background-color: red; border-radius: 50%; top: 0; left: 0; transition: top 0.5s ease-out, left 0.5s ease-out; } ``` 这样,我们就完成了 Vue3 中小抛物线的实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值