基于Vue過(guò)渡狀態(tài)實(shí)例講解
前面的話
Vue 的過(guò)渡系統(tǒng)提供了非常多簡(jiǎn)單的方法設(shè)置進(jìn)入、離開(kāi)和列表的動(dòng)效。那么對(duì)于數(shù)據(jù)元素本身的動(dòng)效呢?包括數(shù)字和運(yùn)算、顏色的顯示、SVG 節(jié)點(diǎn)的位置、元素的大小和其他的屬性等。所有的原始數(shù)字都被事先存儲(chǔ)起來(lái),可以直接轉(zhuǎn)換到數(shù)字。做到這一步,我們就可以結(jié)合 Vue 的響應(yīng)式和組件系統(tǒng),使用第三方庫(kù)來(lái)實(shí)現(xiàn)切換元素的過(guò)渡狀態(tài)
狀態(tài)動(dòng)畫
通過(guò)watcher,能監(jiān)聽(tīng)到任何數(shù)值屬性的數(shù)值更新
<div id="animated-number-demo"> <input v-model.number="number" type="number" step="20"> <p>{{ animatedNumber }}</p> </div> <script src="Tween.js"></script> <script src="vue.js"></script> <script> new Vue({ el: '#animated-number-demo', data: { number: 0, animatedNumber: 0 }, watch: { number: function(newValue, oldValue) { var vm = this; function animate () { if (TWEEN.update()) { requestAnimationFrame(animate) } } new TWEEN.Tween({ tweeningNumber: oldValue }) .easing(TWEEN.Easing.Quadratic.Out) .to({ tweeningNumber: newValue }, 500) .onUpdate(function () { vm.animatedNumber = this.tweeningNumber.toFixed(0) }) .start(); animate() } } }) </script>
當(dāng)把數(shù)值更新時(shí),就會(huì)觸發(fā)動(dòng)畫。這個(gè)是一個(gè)不錯(cuò)的演示,但是對(duì)于不能直接像數(shù)字一樣存儲(chǔ)的值,比如 CSS 中的 color 的值,通過(guò)下面的例子來(lái)通過(guò) Color.js 實(shí)現(xiàn)一個(gè)例子:
<div id="example"> <input v-model="colorQuery" @keyup.enter="updateColor" placeholder="Enter a color"> <button @click="updateColor">Update</button> <p>Preview:</p> <span :style="{ backgroundColor: tweenedCSSColor }" style="display: inline-block;width: 50px;height: 50px;"></span> <p>{{ tweenedCSSColor }}</p> </div> <script src="Tween.js"></script> <script src="vue.js"></script> <script src="color.js"></script> <script> var Color = net.brehaut.Color new Vue({ el: '#example', 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 = '' } } }) </script>
動(dòng)態(tài)狀態(tài)轉(zhuǎn)換
就像 Vue 的過(guò)渡組件一樣,數(shù)據(jù)背后狀態(tài)轉(zhuǎn)換會(huì)實(shí)時(shí)更新,這對(duì)于原型設(shè)計(jì)十分有用。當(dāng)修改一些變量,即使是一個(gè)簡(jiǎn)單的 SVG 多邊形也可以實(shí)現(xiàn)很多難以想象的效果
<style> svg,input[type="range"]{display:block;} </style> <div id="app"> <svg width="200" height="200"> <polygon :points="points" fill="#41B883"></polygon> <circle cx="100" cy="100" r="90" fill=" transparent" stroke="#35495E"></circle> </svg> <label>Sides: {{ sides }}</label> <input type="range" min="3" max="500" v-model.number="sides"> <label>Minimum Radius: {{ minRadius }}%</label> <input type="range" min="0" max="90" v-model.number="minRadius"> <label>Update Interval: {{ updateInterval }} milliseconds</label> <input type="range" min="10" max="2000" v-model.number="updateInterval"> </div> <script type="text/javascript" src="http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/vue.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.5/TweenLite.min.js"></script> <script> new Vue({ el: '#app', data: function () { //默認(rèn)有10條邊 var defaultSides = 10; //默認(rèn)地,stats = [100, 100, 100, 100, 100, 100, 100, 100, 100, 100] var stats = Array.apply(null, { length: defaultSides }) .map(function () { return 100 }) return { stats: stats, points: generatePoints(stats), sides: defaultSides, minRadius: 50, interval: null, updateInterval: 500 } }, watch: { sides: function (newSides, oldSides) { //計(jì)算設(shè)置的邊數(shù)與默認(rèn)的邊數(shù)的差值 var sidesDifference = newSides - oldSides //如果大于默認(rèn)邊數(shù) if (sidesDifference > 0) { //增加相應(yīng)數(shù)量的隨機(jī)值到stats數(shù)組中 for (var i = 1; i <= sidesDifference; i++) { this.stats.push(this.newRandomValue()) } }else{ //否則,計(jì)算出差值 var absoluteSidesDifference = Math.abs(sidesDifference) //從stats數(shù)組末尾減少相應(yīng)數(shù)量的數(shù)組值 for (var i = 1; i <= absoluteSidesDifference; i++) { this.stats.shift() } } }, stats: function (newStats) { TweenLite.to( this.$data, this.updateInterval / 1000, { points: generatePoints(newStats) } ) }, updateInterval: function () { this.resetInterval() } }, mounted: function () { this.resetInterval() }, methods: { //將stats里面的值都變成50-100的隨機(jī)值 randomizeStats: function () { var vm = this this.stats = this.stats.map(function () { return vm.newRandomValue() }) }, newRandomValue: function () { //產(chǎn)生一個(gè)50-100的隨機(jī)半徑 return Math.ceil(this.minRadius + Math.random() * (100 - this.minRadius)) }, //重啟定時(shí)器 resetInterval: function () { var vm = this; clearInterval(this.interval); this.randomizeStats(); this.interval = setInterval(function () { vm.randomizeStats(); }, this.updateInterval) } } }) function valueToPoint (value, index, total) { var x = 0 var y = -value * 0.9 var angle = Math.PI * 2 / total * index var cos = Math.cos(angle) var sin = Math.sin(angle) var tx = x * cos - y * sin + 100 var ty = x * sin + y * cos + 100 return { x: tx, y: ty } } //計(jì)算polygon中的路徑點(diǎn)的值 function generatePoints (stats) { var total = stats.length return stats.map(function (stat, index) { var point = valueToPoint(stat, index, total) return point.x + ',' + point.y }).join(' ') } </script>
組件組織過(guò)渡
管理太多的狀態(tài)轉(zhuǎn)換會(huì)很快的增加 Vue 實(shí)例或者組件的復(fù)雜性,幸好很多的動(dòng)畫可以提取到專用的子組件
<div id="example"> <input v-model.number="firstNumber" type="number" step="20"> + <input v-model.number="secondNumber" type="number" step="20"> = {{ result }} <p> <animated-integer :value="firstNumber"></animated-integer> + <animated-integer :value="secondNumber"></animated-integer> = <animated-integer :value="result"></animated-integer> </p> </div> <script type="text/javascript" src="http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/vue.js"></script> <script type="text/javascript" src="http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/Tween.js"></script> <script> 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() } } }) new Vue({ el: '#example', data: { firstNumber: 20, secondNumber: 40 }, computed: { result: function () { return this.firstNumber + this.secondNumber } } }) </script>
以上這篇基于Vue過(guò)渡狀態(tài)實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue:axios請(qǐng)求本地json路徑錯(cuò)誤問(wèn)題及解決
這篇文章主要介紹了vue:axios請(qǐng)求本地json路徑錯(cuò)誤問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06fetch網(wǎng)絡(luò)請(qǐng)求封裝示例詳解
這篇文章主要介紹了fetch網(wǎng)絡(luò)請(qǐng)求封裝的示例內(nèi)容詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-11-11Vue?實(shí)現(xiàn)新國(guó)標(biāo)紅綠燈效果實(shí)例詳解
這篇文章主要為大家介紹了Vue?實(shí)現(xiàn)新國(guó)標(biāo)紅綠燈效果實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08vue如何解決el-select下拉框顯示ID不顯示label問(wèn)題
這篇文章主要介紹了vue如何解決el-select下拉框顯示ID不顯示label問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06vue實(shí)現(xiàn)多組關(guān)鍵詞對(duì)應(yīng)高亮顯示功能
最近小編遇到這樣的問(wèn)題,多組關(guān)鍵詞,這里實(shí)現(xiàn)了關(guān)鍵詞的背景色與匹配值的字體顏色值相同,下面通過(guò)定義關(guān)鍵詞匹配改變字體顏色,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2019-07-07