新聞中心
這兩天學(xué)習(xí)了Vue.js 感覺渡系統(tǒng)這個(gè)地方知識(shí)點(diǎn)挺多的,而且很重要,所以,今天添加一點(diǎn)小筆記。
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比崇仁網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式崇仁網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋崇仁地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。
Vue 的過渡系統(tǒng)提供了非常多簡單的方法設(shè)置進(jìn)入、離開和列表的動(dòng)效。那么對(duì)于數(shù)據(jù)元素本身的動(dòng)效呢,比如:
- 數(shù)字和運(yùn)算
- 顏色的顯示
- SVG 節(jié)點(diǎn)的位置
- 元素的大小和其他的屬性
所有的原始數(shù)字都被事先存儲(chǔ)起來,可以直接轉(zhuǎn)換到數(shù)字。做到這一步,我們就可以結(jié)合 Vue 的響應(yīng)式和組件系統(tǒng),使用第三方庫來實(shí)現(xiàn)切換元素的過渡狀態(tài)。
狀態(tài)動(dòng)畫和watcher
通過 watcher 我們能監(jiān)聽到任何數(shù)值屬性的數(shù)值更新??赡苈犉饋砗艹橄?,所以讓我們先來看看使用 Tweenjs 一個(gè)例子。
Js代碼中引入:
示例代碼:
{{animateNumber}}
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() } } })
運(yùn)行結(jié)果:
當(dāng)你把數(shù)值更新時(shí),就會(huì)觸發(fā)動(dòng)畫。這個(gè)是一個(gè)不錯(cuò)的演示,但是對(duì)于不能直接像數(shù)字一樣存儲(chǔ)的值,比如 CSS 中的 color 的值,通過下面的例子我們來通過 Color.js 實(shí)現(xiàn)一個(gè)例子。
js需要添加以下引用:
示例代碼:
Preview:
{{tweenedCSSColor}}
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 = '' } } })
運(yùn)行結(jié)果:
通過組件組織過渡
管理太多的狀態(tài)轉(zhuǎn)換會(huì)很快的增加 Vue 實(shí)例或者組件的復(fù)雜性,幸好很多的動(dòng)畫可以提取到專用的子組件。
我們來將之前的示例改寫一下:
+ = {{result}}
+ =
Vue.component('animate-integer',{
template:'{{tweeningValue}}',
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
}
}
})
運(yùn)行結(jié)果:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
名稱欄目:Vue學(xué)習(xí)筆記進(jìn)階篇之過渡狀態(tài)詳解
轉(zhuǎn)載來于:http://fisionsoft.com.cn/article/geohhc.html