分析
這種不固定起始位置的動(dòng)畫,自然不能用 gif 圖,所以只能用原生代碼實(shí)現(xiàn)
那我們有什么工具來實(shí)現(xiàn)動(dòng)畫呢?
小程序提供了 JS API createAnimation 來創(chuàng)建動(dòng)畫
CSS transition
工具有了,我們再看一下什么是拋物線。
這里我們只討論水平拋物線,水平拋物線從數(shù)學(xué)原理上來說就是【水平勻速、垂直加速的運(yùn)動(dòng)】,轉(zhuǎn)換成代碼層面就是在動(dòng)畫效果 timingFunction 中,水平動(dòng)畫采用 linear ,垂直動(dòng)畫采用 ease-in
所以我們需要把這個(gè)拋物線動(dòng)畫分解成 兩個(gè) 同時(shí) 進(jìn)行但 不同動(dòng)畫效果 的動(dòng)畫。
實(shí)現(xiàn)
(一)小程序的實(shí)現(xiàn)
JS:
cartAnimation(x, y) { // x y 為手指點(diǎn)擊的坐標(biāo),即球的起始坐標(biāo) let self = this, cartY = app.globalData.winHeight - 50, // 結(jié)束位置(購物車圖片)縱坐標(biāo) cartX = 50, // 結(jié)束位置(購物車圖片)的橫坐標(biāo) animationX = flyX(cartX, x), // 創(chuàng)建球的橫向動(dòng)畫 animationY = flyY(cartY, y) // 創(chuàng)建球的縱向動(dòng)畫 this.setData({ ballX: x, ballY: y, showBall: true }) setTimeoutES6(100).then(() => { // 100 ms 延時(shí),確保球已經(jīng)到位并顯示 self.setData({ animationX: animationX.export(), animationY: animationY.export(), }) return setTimeoutES6(400) // 400 ms 是球的拋物線動(dòng)畫時(shí)長 }).then(() => { // 400 ms 延時(shí)后隱藏球 this.setData({ showBall: false, }) }) }function setTimeoutES6(sec) { // Promise 化 setTimeout return new Promise((resolve, reject) => { setTimeout(() => {resolve()}, sec) }) }function flyX(cartX, oriX) { // 水平動(dòng)畫 let animation = wx.createAnimation({ duration: 400, timingFunction: 'linear', }) animation.left(cartX).step() return animation }function flyY(cartY, oriY) { // 垂直動(dòng)畫 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>
translate 優(yōu)化
據(jù)我所知,用 transform: translate() 來實(shí)現(xiàn)的動(dòng)畫會(huì)比 top & left 性能更優(yōu),但實(shí)現(xiàn)下來卻沒那么容易咯。
研究來研究去,發(fā)現(xiàn) translate 的做法比 top & left 的做法多了一步,就是需要將小球的 translate 位移還原(否則 translate 一直有值),才能保證下一次的位移從點(diǎn)擊的位置開始
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 恢復(fù)默認(rèn)值 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 的實(shí)現(xiàn)
除了小程序之外,前端日常開發(fā)更多的當(dāng)然還是 H5,下面我將用 CSS3 transition 的方法來實(shí)現(xiàn)
<!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 水平拋物線動(dòng)畫</title></head><body style="width:100%;height:100%;"> <p id="ball"></p></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>
相關(guān)推薦:
JS拋物線動(dòng)畫操作實(shí)例分享
關(guān)于html5中圖片拋物線運(yùn)動(dòng)技巧分享
Javascript中彈性勢能動(dòng)畫之關(guān)于拋物線運(yùn)動(dòng)的代碼案例
以上就是JS實(shí)現(xiàn)拋物線動(dòng)畫的代碼實(shí)例的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!