</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()










