學習心得,
組件(六),
直接上代碼了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>狀態過渡</title>
<script src="../js/vue.js"></script>
</head>
<body>
<!--狀態過渡-->
<!--Vue 的過渡系統提供了非常多簡單的方法設置進入、離開和列表的動效。-->
<!--那么對于數據元素本身的動效呢,比如:-->
<!--數字和運算-->
<!--顏色的顯示-->
<!--SVG 節點的位置-->
<!--元素的大小和其他的屬性-->
<!--所有的原始數字都被事先存儲起來,可以直接轉換到數字。-->
<!--做到這一步,我們就可以結合 Vue 的響應式和組件系統,使用第三方庫來實現切換元素的過渡狀態。-->
<!--狀態動畫與觀察者-->
<!--通過觀察者我們能監聽到任何數值屬性的數值更新。-->
<!--可能聽起來很抽象,所以讓我們先來看看使用 Tweenjs 一個例子:-->
<script src="https://cdn.jsdelivr.net/npm/tween.js@16.3.4"></script>
<div id="app-1">
<input v-model.number="number" type="number" step="20">
<p>{{ animatedNumber }}</p>
</div>
<hr>
<script>
var app1 = new Vue({
el:"#app-1",
data:{
number:0,
animatedNumber:0
},
watch:{
number:function(newValue ,oldValue){
console.log(newValue);
console.log(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>
<!--當你把數值更新時,就會觸發動畫。-->
<!--這個是一個不錯的演示,但是對于不能直接像數字一樣存儲的值,-->
<!--比如 CSS 中的 color 的值,通過下面的例子我們來通過 Color.js 實現一個例子:-->
<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="app-2">
<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>
<hr>
<script>
var Color = net.brehaut.Color
new Vue({
el: '#app-2',
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>
<style>
.example-7-color-preview {
display: inline-block;
width: 50px;
height: 50px;
}
</style>
<!--把過渡放到組件里-->
<!--管理太多的狀態過渡會很快的增加 Vue 實例或者組件的復雜性,幸好很多的動畫可以提取到專用的子組件。-->
<!--我們來將之前的示例改寫一下:-->
<div id="app-3">
<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>
<hr>
<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()
}
}
})
// 所有的復雜度都已經從 Vue 的主實例中移除!
new Vue({
el: '#app-3',
data: {
firstNumber: 20,
secondNumber: 40
},
computed: {
result: function () {
return this.firstNumber + this.secondNumber
}
}
})
// 我們能在組件中結合使用這一節講到各種過渡策略和 Vue 內建的過渡系統。
// 總之,對于完成各種過渡動效幾乎沒有阻礙。
</script>
</body>
</html>