author: 陈家宾 email: 617822642@qq.com date: 2018/2/24
在作小程序的项目中,须要在添加购物车的时候,增长抛物线小球动画。html
先给你们看下效果图(其实已是实现后的效果了,顺便给本身公司打广告了哈哈)前端
这种不固定起始位置的动画,天然不能用 gif 图,因此只能用原生代码实现git
那咱们有什么工具来实现动画呢?github
createAnimation
来建立动画工具备了,咱们再看一下什么是抛物线。小程序
这里咱们只讨论水平抛物线,水平抛物线从数学原理上来讲就是【水平匀速、垂直加速的运动】,转换成代码层面就是在动画效果 timingFunction
中,水平动画采用 linear
,垂直动画采用 ease-in
app
因此咱们须要把这个抛物线动画分解成 两个 同时 进行但 不一样动画效果 的动画。工具
JS:性能
cartAnimation(x, y) { // x y 为手指点击的坐标,即球的起始坐标 let self = this, cartY = app.globalData.winHeight - 50, // 结束位置(购物车图片)纵坐标 cartX = 50, // 结束位置(购物车图片)的横坐标 animationX = flyX(cartX, x), // 建立球的横向动画 animationY = flyY(cartY, y) // 建立球的纵向动画 this.setData({ ballX: x, ballY: y, showBall: true }) setTimeoutES6(100).then(() => { // 100 ms 延时,确保球已经到位并显示 self.setData({ animationX: animationX.export(), animationY: animationY.export(), }) return setTimeoutES6(400) // 400 ms 是球的抛物线动画时长 }).then(() => { // 400 ms 延时后隐藏球 this.setData({ showBall: false, }) }) } function setTimeoutES6(sec) { // Promise 化 setTimeout return new Promise((resolve, reject) => { setTimeout(() => {resolve()}, sec) }) } function flyX(cartX, oriX) { // 水平动画 let animation = wx.createAnimation({ duration: 400, timingFunction: 'linear', }) animation.left(cartX).step() return animation } function flyY(cartY, oriY) { // 垂直动画 let animation = wx.createAnimation({ duration: 400, timingFunction: 'ease-in', }) animation.top(cartY).step() return animation }
HTML:优化
<view animation="{{animationY}}" style="position:fixed;top:{{ballY}}px;" hidden="{{!showBall}}"> <view class="ball" animation="{{animationX}}" style="position:fixed;left:{{ballX}}px;"></view> </view>
据我所知,用 transform: translate()
来实现的动画会比 top & left 性能更优,但实现下来却没那么容易咯。动画
研究来研究去,发现 translate 的作法比 top & left 的作法多了一步,就是须要将小球的 translate 位移还原(不然 translate 一直有值),才能保证下一次的位移从点击的位置开始
cartAnimation(x, y) { let self = this, cartY = app.globalData.winHeight - 50, cartX = 50, animationX = flyX(cartX, x), animationY = flyY(cartY, y) this.setData({ leftNum: x, topNum: y, showBall: true }) setTimeoutES6(100).then(() => { self.setData({ animationDataX: animationX.export(), animationDataY: animationY.export(), }) return setTimeoutES6(400) }).then(() => { this.setData({ showBall: false, animationX: flyX(0, 0, 0).export(), // 还原小球位置,即 translate 恢复默认值 animationY: flyY(0, 0, 0).export(), }) }) } function flyX(cartX,oriX,duration) { let animation = wx.createAnimation({ duration: duration||400, timingFunction: 'linear', }) animation.translateX(cartX-oriX).step() return animation } function flyY(cartY,oriY,duration) { let animation = wx.createAnimation({ duration: duration||400, timingFunction: 'ease-in', }) animation.translateY(cartY-oriY).step() return animation }
HTML 部分不变
除了小程序以外,前端平常开发更多的固然仍是 H5,下面我将用 CSS3 transition 的方法来实现
<!DOCTYPE html> <html lang="en" style="width:100%;height:100%;"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width"> <style> * { padding: 0; margin: 0; } #ball { width:12px; height:12px; background: #5EA345; border-radius: 50%; position: fixed; transition: left 1s linear, top 1s ease-in; } </style> <title>CSS3 水平抛物线动画</title> </head> <body style="width:100%;height:100%;"> <div id="ball"></div> </body> <script> var $ball = document.getElementById('ball'); document.body.onclick = function (evt) { console.log(evt.pageX,evt.pageY) $ball.style.top = evt.pageY+'px'; $ball.style.left = evt.pageX+'px'; $ball.style.transition = 'left 0s, top 0s'; setTimeout(()=>{ $ball.style.top = window.innerHeight+'px'; $ball.style.left = '0px'; $ball.style.transition = 'left 1s linear, top 1s ease-in'; }, 20) } </script> </html>
还有体验连接哦,点我
至此,水平抛物线动画的实现就介绍得差很少啦,嘻嘻!!