欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS拋物線動(dòng)畫實(shí)例制作

 更新時(shí)間:2018年02月24日 15:43:45   作者:henry_chen  
本篇文章給大家詳細(xì)分析了JS拋物線動(dòng)畫制作過程以及相關(guān)的代碼實(shí)例分享,有興趣的朋友參考下。

在做無人便利小程序的項(xiàng)目中,某一天產(chǎn)品說要像某產(chǎn)商產(chǎn)品學(xué)習(xí),給添加購物車增加拋物線小球動(dòng)畫。好吧,產(chǎn)品你最大,做!

先給大家看下效果圖

分析

這種不固定起始位置的動(dòng)畫,自然不能用 gif 圖,所以只能用原生代碼實(shí)現(xiàn)

那我們有什么工具來實(shí)現(xiàn)動(dòng)畫呢?

小程序提供了 JS API createAnimation 來創(chuàng)建動(dòng)畫

CSS animation

工具有了,我們再看一下什么是拋物線。

這里我們只討論水平拋物線,水平拋物線從數(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 秒延時(shí),確保球已經(jīng)到位并顯示
    self.setData({
      animationX: animationX.export(),
      animationY: animationY.export(),
    })
    return setTimeoutES6(400) // 400 是球的拋物線動(dòng)畫時(shí)長
  }).then(() => { // 400 秒延時(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>

據(jù)我所知,用 transform: transtate() 來實(shí)現(xiàn)的動(dòng)畫會比 top & left 性能更優(yōu),但嘗試下來發(fā)現(xiàn)并不能做到理想的交互效果,期待后續(xù)繼續(xù)研究吧

H5 的實(shí)現(xiàn)

// todo

相關(guān)文章

最新評論