最近在做一个购物车的项目,里面涉及到按钮按下会有小球抛物线的动画。
大概就是,使用了transition属性,用js让小球有一个间隔时间产生变化,触发动画。动画使用了贝塞尔曲线的效果固可以让小球在水平和垂直方向上产生不同的速度,从而实现抛物线效果。
<!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> body{ margin: 0; height: 100vh; background-color: crimson; } </style> </head> <body> </body> <script> document.body.onclick=(e)=>{ console.log(e.clientX,e.clientY); let ball = document.createElement('div') ball.style.width = '66px' ball.style.height = '66px' ball.style.backgroundColor = 'dodgerblue' ball.style.position = 'fixed' ball.style.left = e.clientX+'px' ball.style.borderRadius = "50%" ball.style.top = e.clientY+'px' ball.style.transition ='left .3s linear, top .3s cubic-bezier(0.5, -0.5, 1, 1)' document.body.appendChild(ball) setTimeout(() => { ball.style.left = '500px' ball.style.top = '900px' }, 17); setTimeout(() => { ball.remove() }, 317); } </script> </html>
复制