新聞中心
Vue 的過(guò)渡系統(tǒng)提供了非常多簡(jiǎn)單的方法設(shè)置進(jìn)入、離開(kāi)和列表的動(dòng)效。那么對(duì)于數(shù)據(jù)元素本身的動(dòng)效呢,比如:

創(chuàng)新互聯(lián)是專(zhuān)業(yè)的商水網(wǎng)站建設(shè)公司,商水接單;提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行商水網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
- 數(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)畫(huà) 與 watcher
通過(guò) watcher 我們能監(jiān)聽(tīng)到任何數(shù)值屬性的數(shù)值更新。可能聽(tīng)起來(lái)很抽象,所以讓我們先來(lái)看看使用Tweenjs一個(gè)例子:
{{ animatedNumber }}
new Vue({
el: '#animated-number-demo',
data: {
number: 0,
animatedNumber: 0
},
watch: {
number: function(newValue, oldValue) {
var vm = this
function animate (time) {
requestAnimationFrame(animate)
TWEEN.update(time)
}
new TWEEN.Tween({ tweeningNumber: oldValue })
.easing(TWEEN.Easing.Quadratic.Out)
.to({ tweeningNumber: newValue }, 500)
.onUpdate(function () {
vm.animatedNumber = this.tweeningNumber.toFixed(0)
})
.start()
animate()
}
}
})
0
當(dāng)你把數(shù)值更新時(shí),就會(huì)觸發(fā)動(dòng)畫(huà)。這個(gè)是一個(gè)不錯(cuò)的演示,但是對(duì)于不能直接像數(shù)字一樣存儲(chǔ)的值,比如 CSS 中的 color 的值,通過(guò)下面的例子我們來(lái)通過(guò) Color.js 實(shí)現(xiàn)一個(gè)例子:
Preview:
{{ tweenedCSSColor }}
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 (time) {
requestAnimationFrame(animate)
TWEEN.update(time)
}
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;
}
Update
Preview:
#000000
動(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)很多難以想象的效果。
Sides: 468 Minimum Radius: 50% Update Interval: 500 milliseconds
查看該 fiddle,了解上面演示的完整代碼。
通過(guò)組件組織過(guò)渡
管理太多的狀態(tài)轉(zhuǎn)換的很快會(huì)接近到 Vue 實(shí)例或者組件的復(fù)雜性,幸好很多的動(dòng)畫(huà)可以提取到專(zhuān)用的子組件。我們來(lái)將之前的示例改寫(xiě)一下:
+
=
{{ result }}
+
=
// 這種復(fù)雜的補(bǔ)間動(dòng)畫(huà)邏輯可以被復(fù)用
// 任何整數(shù)都可以執(zhí)行動(dòng)畫(huà)
// 組件化使我們的界面十分清晰
// 可以支持更多更復(fù)雜的動(dòng)態(tài)過(guò)渡
// strategies.
Vue.component('animated-integer', {
template: '{{ tweeningValue }}',
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 (time) {
requestAnimationFrame(animate)
TWEEN.update(time)
}
new TWEEN.Tween({ tweeningValue: startValue })
.to({ tweeningValue: endValue }, 500)
.onUpdate(function () {
vm.tweeningValue = this.tweeningValue.toFixed(0)
})
.start()
animate()
}
}
})
// All complexity has now been removed from the main Vue instance!
new Vue({
el: '#example-8',
data: {
firstNumber: 20,
secondNumber: 40
},
computed: {
result: function () {
return this.firstNumber + this.secondNumber
}
}
})
+ = 60
20 + 40 = 60
我們能在組件中結(jié)合使用這一節(jié)講到各種過(guò)渡策略和 Vue 內(nèi)建的過(guò)渡系統(tǒng)??傊瑢?duì)于完成各種過(guò)渡動(dòng)效幾乎沒(méi)有阻礙。
本文標(biāo)題:創(chuàng)新互聯(lián)VUE2教程:Vue.js2.0過(guò)渡狀態(tài)
分享路徑:http://www.dlmjj.cn/article/dppjddj.html


咨詢(xún)
建站咨詢(xún)
