狀態(tài)過(guò)渡
Vue 的過(guò)渡系統(tǒng)提供了非常多簡(jiǎn)單的方法設(shè)置進(jìn)入、離開和列表的動(dòng)效。
目錄
Vue 的過(guò)渡系統(tǒng)提供了非常多簡(jiǎn)單的方法設(shè)置進(jìn)入、離開和列表的動(dòng)效。那么對(duì)于數(shù)據(jù)元素本身的動(dòng)效呢,比如:
數(shù)字和運(yùn)算
顏色的顯示
SVG 節(jié)點(diǎn)的位置
元素的大小和其他的屬性
這些數(shù)據(jù)要么本身就以數(shù)值形式存儲(chǔ),要么可以轉(zhuǎn)換為數(shù)值。有了這些數(shù)值后,我們就可以結(jié)合 Vue 的響應(yīng)式和組件系統(tǒng),使用第三方庫(kù)來(lái)實(shí)現(xiàn)切換元素的過(guò)渡狀態(tài)。
狀態(tài)動(dòng)畫與偵聽器
通過(guò)偵聽器我們能監(jiān)聽到任何數(shù)值屬性的數(shù)值更新??赡苈犉饋?lái)很抽象,所以讓我們先來(lái)看看使用 GreenSock 一個(gè)例子:
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.3/TweenMax.min.js"></script> <div id="animated-number-demo"> <input v-model.number="number" type="number" step="20"> <p>{{ animatedNumber }}</p> </div>
new Vue({ el: '#animated-number-demo', data: { number: 0, tweenedNumber: 0 }, computed: { animatedNumber: function() { return this.tweenedNumber.toFixed(0); } }, watch: { number: function(newValue) { TweenLite.to(this.$data, 0.5, { tweenedNumber: newValue }); } } })
當(dāng)你把數(shù)值更新時(shí),就會(huì)觸發(fā)動(dòng)畫。這個(gè)是一個(gè)不錯(cuò)的演示,但是對(duì)于不能直接像數(shù)字一樣存儲(chǔ)的值,比如 CSS 中的 color 的值,通過(guò)下面的例子我們來(lái)通過(guò) Tween.js 和 Color.js 實(shí)現(xiàn)一個(gè)例子:
<script src="https://cdn.jsdelivr.net/npm/tween.js@16.3.4"></script> <script src="https://cdn.jsdelivr.net/npm/color-js@1.0.3"></script> <div id="example-7"> <input v-model="colorQuery" v-on:keyup.enter="updateColor" placeholder="Enter a color" > <button v-on:click="updateColor">Update</button> <p>Preview:</p> <span v-bind:style="{ backgroundColor: tweenedCSSColor }" class="example-7-color-preview" ></span> <p>{{ tweenedCSSColor }}</p> </div>
var Color = net.brehaut.Color new Vue({ el: '#example-7', 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 = '' } } })
.example-7-color-preview { display: inline-block; width: 50px; height: 50px; }
動(dòng)態(tài)狀態(tài)過(guò)渡
就像 Vue 的過(guò)渡組件一樣,數(shù)據(jù)背后狀態(tài)過(guò)渡會(huì)實(shí)時(shí)更新,這對(duì)于原型設(shè)計(jì)十分有用。當(dāng)你修改一些變量,即使是一個(gè)簡(jiǎn)單的 SVG 多邊形也可實(shí)現(xiàn)很多難以想象的效果。
上述 demo 背后的代碼可以通過(guò)這個(gè) fiddle 進(jìn)行詳閱。
把過(guò)渡放到組件里
管理太多的狀態(tài)過(guò)渡會(huì)很快的增加 Vue 實(shí)例或者組件的復(fù)雜性,幸好很多的動(dòng)畫可以提取到專用的子組件。
我們來(lái)將之前的示例改寫一下:
<script src="https://cdn.jsdelivr.net/npm/tween.js@16.3.4"></script> <div id="example-8"> <input v-model.number="firstNumber" type="number" step="20"> + <input v-model.number="secondNumber" type="number" step="20"> = {{ result }} <p> <animated-integer v-bind:value="firstNumber"></animated-integer> + <animated-integer v-bind:value="secondNumber"></animated-integer> = <animated-integer v-bind:value="result"></animated-integer> </p> </div>
// 這種復(fù)雜的補(bǔ)間動(dòng)畫邏輯可以被復(fù)用 // 任何整數(shù)都可以執(zhí)行動(dòng)畫 // 組件化使我們的界面十分清晰 // 可以支持更多更復(fù)雜的動(dòng)態(tài)過(guò)渡 // 策略。 Vue.component('animated-integer', { template: '<span>{{ tweeningValue }}</span>', props: { value: { type: Number, required: true } }, data: function () { return { tweeningValue: 0 } }, watch: { value: function (newValue, oldValue) { this.tween(oldValue, newValue) } }, mounted: function () { this.tween(0, this.value) }, 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() } } }) // 所有的復(fù)雜度都已經(jīng)從 Vue 的主實(shí)例中移除! new Vue({ el: '#example-8', data: { firstNumber: 20, secondNumber: 40 }, computed: { result: function () { return this.firstNumber + this.secondNumber } } })
我們能在組件中結(jié)合使用這一節(jié)講到各種過(guò)渡策略和 Vue 內(nèi)建的過(guò)渡系統(tǒng)。總之,對(duì)于完成各種過(guò)渡動(dòng)效幾乎沒(méi)有阻礙。
賦予設(shè)計(jì)以生命
只要一個(gè)動(dòng)畫,就可以帶來(lái)生命。不幸的是,當(dāng)設(shè)計(jì)師創(chuàng)建圖標(biāo)、logo 和吉祥物的時(shí)候,他們交付的通常都是圖片或靜態(tài)的 SVG。所以,雖然 GitHub 的章魚貓、Twitter 的小鳥以及其它許多 logo 類似于生靈,它們看上去實(shí)際上并不是活著的。
Vue 可以幫到你。因?yàn)?SVG 的本質(zhì)是數(shù)據(jù),我們只需要這些動(dòng)物興奮、思考或警戒的樣例。然后 Vue 就可以輔助完成這幾種狀態(tài)之間的過(guò)渡動(dòng)畫,來(lái)制作你的歡迎頁(yè)面、加載指示、以及更加帶有情感的提示。
Sarah Drasner 展示了下面這個(gè) demo,這個(gè) demo 結(jié)合了時(shí)間和交互相關(guān)的狀態(tài)改變: