Vue學習筆記進階篇——過渡狀態
本文為轉載,原文:Vue學習筆記進階篇——過渡狀態
Vue 的過渡系統提供了非常多簡單的方法設置進入、離開和列表的動效。那麽對於數據元素本身的動效呢,比如:
- 數字和運算
- 顏色的顯示
- SVG 節點的位置
- 元素的大小和其他的屬性
所有的原始數字都被事先存儲起來,可以直接轉換到數字。做到這一步,我們就可以結合 Vue 的響應式和組件系統,使用第三方庫來實現切換元素的過渡狀態。
狀態動畫和watcher
通過 watcher 我們能監聽到任何數值屬性的數值更新。可能聽起來很抽象,所以讓我們先來看看使用 Tweenjs
一個例子。
Js代碼中引入:
<script src="https:[email protected]
示例代碼:
<div id="app1"> <input v-model.number="number" type="number" step="20"> <p>{{animateNumber}}</p> </div>
var app1 = new Vue({ el:‘#app1‘, data:{ number:0, animateNumber:0 }, watch:{ number:function(newVal, oldVal) { var vm = this function animate() { if (TWEEN.update()){ requestAnimationFrame(animate) } } new TWEEN.Tween({tweeningNumber:oldVal}) .easing(TWEEN.Easing.Quadratic.Out) .to({tweeningNumber:newVal},500) .onUpdate(function () { vm.animateNumber = this.tweeningNumber.toFixed(0) }) .onComplete(function () { cancelAnimationFrame(animate) }) .start() animate() } } })
運行結果:
當你把數值更新時,就會觸發動畫。這個是一個不錯的演示,但是對於不能直接像數字一樣存儲的值,比如 CSS 中的 color 的值,通過下面的例子我們來通過 Color.js
實現一個例子。
js需要添加以下引用:
<script src="https:[email protected]"></script> <script src="https:[email protected]/color.js"></script>
示例代碼:
<div id="app-color"> <input v-model="colorQuery" @keyup.enter="updateColor" placeholder="Enter a color"> <button @click="updateColor">Update</button> <p>Preview:</p> <span :style="{backgroundColor:tweenedCSSColor}" class="color-preview"> </span> <p>{{tweenedCSSColor}}</p> </div>
var Color = net.brehaut.Color new Vue({ el:‘#app-color‘, data:{ colorQuery:‘‘, color:{ red:0, green:0, blue:0, alpha:1 }, tweenedColor:{} }, created:function () { this.tweenedColor = Object.assign({}, this.color) }, watch:{ color:function () { function animate() { if (TWEEN.update()){ requestAnimationFrame(animate) } } new TWEEN.Tween(this.tweenedColor) .to(this.color, 750) .start() animate() } }, computed:{ tweenedCSSColor:function () { return new Color({ red:this.tweenedColor.red, green:this.tweenedColor.green, blue:this.tweenedColor.blue, alpha:this.tweenedColor.alpha }).toCSS() } }, methods:{ updateColor:function () { this.color = new Color(this.colorQuery).toRGB() this.colorQuery = ‘‘ } } })
運行結果:
通過組件組織過渡
管理太多的狀態轉換會很快的增加 Vue 實例或者組件的復雜性,幸好很多的動畫可以提取到專用的子組件。
我們來將之前的示例改寫一下:
<div id="app"> <input v-model.number="firstNumber" type="number" step="20"> + <input v-model.number="secondNumber" type="number" step="20"> = {{result}} <p> <animate-integer :value="firstNumber"></animate-integer> + <animate-integer :value="secondNumber"></animate-integer> = <animate-integer :value="result"></animate-integer> </p> </div>
Vue.component(‘animate-integer‘,{ template:‘<span>{{tweeningValue}}</span>‘, props:{ value:{ type:Number, required:true } }, data:function () { return {tweeningValue:0} }, mounted:function () { this.tween(0, this.value) }, watch:{ value:function (newVal, oldVal) { this.tween(oldVal, newVal) } }, methods:{ tween:function (startValue, endValue) { var vm = this function animate() { if(TWEEN.update()){ requestAnimationFrame(animate) } } new TWEEN.Tween({tweeningValue:startValue}) .to({tweeningValue:endValue}, 500) .onUpdate(function () { vm.tweeningValue = this.tweeningValue.toFixed(0) }).start() animate() } } }) new Vue({ el:‘#app‘, data:{ firstNumber:20, secondNumber:40 }, computed:{ result:function () { return this.firstNumber + this.secondNumber } } })
運行結果:
完
原創文章,轉載請註明出處
上一節:Vue學習筆記進階篇——列表過渡及其他
返回目錄
下一節:Vue學習筆記進階篇——Render函數
Vue學習筆記進階篇——過渡狀態