可观察到的敲除是否可以实现数字动画行为?
Is numeric animation behaviour possible with knockout observable?
例如你有这样的简单模型:
var ViewModel = function() {
var timer,
self = this;
this.value = ko.observable(10);
this.value2 = ko.observable(10);
this.value.subscribe(function(newValue){
clearInterval(timer)
timer = setInterval(function(){
if (self.value2() == newValue) {
clearInterval(timer);
return;
}
self.value2(self.value2() + 1);
}, 100);
});
this.update = function(){
this.value(this.value() + 10);
};
};
ko.applyBindings(new ViewModel());
body { font-family: arial; font-size: 14px; }
.liveExample { padding: 1em; background-color: #EEEEDD; border: 1px solid #CCC; max-width: 655px; }
.liveExample input { font-family: Arial; }
.liveExample b { font-weight: bold; }
.liveExample p { margin-top: 0.9em; margin-bottom: 0.9em; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class='liveExample'>
<p>Value: <input data-bind='value: value' /></p>
<p>Animated value: <input data-bind='value: value2' /></p>
<button data-bind="click: update">
+10
</button>
</div>
每次按下按钮 value
都会递增,并且 value2
会使用数字动画进行动画处理(例如在 Qt QML 中)。在这个示例中,这完成了抛出新变量和自制计时器。
问题
这里是否有任何方法可以通过自定义绑定或自定义扩展程序进行此类行为?
我已经绘制了自定义 "animatedValue" 绑定,希望对您有所帮助:
function createValueAccessor(val) {
var displayValue = ko.observable(val());
var timer;
val.subscribe(function(newValue) {
clearInterval(timer)
timer = setInterval(function(){
if(displayValue() == newValue) {
clearInterval(timer);
} else {
displayValue(displayValue() + 1);
}
}, 100);
});
return function() {
return displayValue;
}
}
// Binding handler
ko.bindingHandlers.animatedValue = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var value = valueAccessor();
ko.bindingHandlers.value.init(element, createValueAccessor(value), allBindingsAccessor, viewModel);
}
};
var ViewModel = function() {
var timer,
self = this;
this.value = ko.observable(10);
this.value2 = ko.observable(10);
this.value.subscribe(function(newValue){
clearInterval(timer)
timer = setInterval(function(){
if (self.value2() == newValue) {
clearInterval(timer);
return;
}
self.value2(self.value2() + 1);
}, 100);
});
this.update = function(){
this.value(this.value() + 10);
};
};
function createValueAccessor(val) {
var displayValue = ko.observable(val());
var timer;
val.subscribe(function(newValue) {
clearInterval(timer)
timer = setInterval(function(){
if(displayValue() == newValue) {
clearInterval(timer);
} else {
displayValue(displayValue() + 1);
}
}, 100);
});
return function() {
return displayValue;
}
}
// Binding handler
ko.bindingHandlers.animatedValue = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var value = valueAccessor();
ko.bindingHandlers.value.init(element, createValueAccessor(value), allBindingsAccessor, viewModel);
}
};
ko.bindingHandlers.animatedText = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var value = valueAccessor();
var displayValue = ko.observable(value());
var timer;
value.subscribe(function(newValue) {
clearInterval(timer)
timer = setInterval(function() {
if(displayValue() == newValue) {
clearInterval(timer);
} else {
displayValue(displayValue() + 1);
ko.bindingHandlers.text.update(element, function() { return displayValue; });
}
}, 100);
});
ko.bindingHandlers.text.update(element, function() { return displayValue; });
}
};
ko.applyBindings(new ViewModel());
body { font-family: arial; font-size: 14px; }
.liveExample { padding: 1em; background-color: #EEEEDD; border: 1px solid #CCC; max-width: 655px; }
.liveExample input { font-family: Arial; }
.liveExample b { font-weight: bold; }
.liveExample p { margin-top: 0.9em; margin-bottom: 0.9em; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class='liveExample'>
<p>Value: <input data-bind='value: value' /></p>
<p>Animated value: <input data-bind='value: value2' /></p>
<p>Animated value via animatedText binding: <span data-bind='animatedText: value'></span></p>
<p>Animated value via binding: <input data-bind='animatedValue: value' /></p>
<button data-bind="click: update">
+10
</button>
</div>
更新 1
我添加了 "animatedText" 自定义绑定处理程序。
ko.bindingHandlers.animatedText = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var value = valueAccessor();
var displayValue = ko.observable(value());
var timer;
value.subscribe(function(newValue) {
clearInterval(timer)
timer = setInterval(function() {
if(displayValue() == newValue) {
clearInterval(timer);
} else {
displayValue(displayValue() + 1);
ko.bindingHandlers.text.update(element, function() { return displayValue; });
}
}, 100);
});
ko.bindingHandlers.text.update(element, function() { return displayValue; });
}
};
例如你有这样的简单模型:
var ViewModel = function() {
var timer,
self = this;
this.value = ko.observable(10);
this.value2 = ko.observable(10);
this.value.subscribe(function(newValue){
clearInterval(timer)
timer = setInterval(function(){
if (self.value2() == newValue) {
clearInterval(timer);
return;
}
self.value2(self.value2() + 1);
}, 100);
});
this.update = function(){
this.value(this.value() + 10);
};
};
ko.applyBindings(new ViewModel());
body { font-family: arial; font-size: 14px; }
.liveExample { padding: 1em; background-color: #EEEEDD; border: 1px solid #CCC; max-width: 655px; }
.liveExample input { font-family: Arial; }
.liveExample b { font-weight: bold; }
.liveExample p { margin-top: 0.9em; margin-bottom: 0.9em; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class='liveExample'>
<p>Value: <input data-bind='value: value' /></p>
<p>Animated value: <input data-bind='value: value2' /></p>
<button data-bind="click: update">
+10
</button>
</div>
每次按下按钮 value
都会递增,并且 value2
会使用数字动画进行动画处理(例如在 Qt QML 中)。在这个示例中,这完成了抛出新变量和自制计时器。
问题
这里是否有任何方法可以通过自定义绑定或自定义扩展程序进行此类行为?
我已经绘制了自定义 "animatedValue" 绑定,希望对您有所帮助:
function createValueAccessor(val) {
var displayValue = ko.observable(val());
var timer;
val.subscribe(function(newValue) {
clearInterval(timer)
timer = setInterval(function(){
if(displayValue() == newValue) {
clearInterval(timer);
} else {
displayValue(displayValue() + 1);
}
}, 100);
});
return function() {
return displayValue;
}
}
// Binding handler
ko.bindingHandlers.animatedValue = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var value = valueAccessor();
ko.bindingHandlers.value.init(element, createValueAccessor(value), allBindingsAccessor, viewModel);
}
};
var ViewModel = function() {
var timer,
self = this;
this.value = ko.observable(10);
this.value2 = ko.observable(10);
this.value.subscribe(function(newValue){
clearInterval(timer)
timer = setInterval(function(){
if (self.value2() == newValue) {
clearInterval(timer);
return;
}
self.value2(self.value2() + 1);
}, 100);
});
this.update = function(){
this.value(this.value() + 10);
};
};
function createValueAccessor(val) {
var displayValue = ko.observable(val());
var timer;
val.subscribe(function(newValue) {
clearInterval(timer)
timer = setInterval(function(){
if(displayValue() == newValue) {
clearInterval(timer);
} else {
displayValue(displayValue() + 1);
}
}, 100);
});
return function() {
return displayValue;
}
}
// Binding handler
ko.bindingHandlers.animatedValue = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var value = valueAccessor();
ko.bindingHandlers.value.init(element, createValueAccessor(value), allBindingsAccessor, viewModel);
}
};
ko.bindingHandlers.animatedText = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var value = valueAccessor();
var displayValue = ko.observable(value());
var timer;
value.subscribe(function(newValue) {
clearInterval(timer)
timer = setInterval(function() {
if(displayValue() == newValue) {
clearInterval(timer);
} else {
displayValue(displayValue() + 1);
ko.bindingHandlers.text.update(element, function() { return displayValue; });
}
}, 100);
});
ko.bindingHandlers.text.update(element, function() { return displayValue; });
}
};
ko.applyBindings(new ViewModel());
body { font-family: arial; font-size: 14px; }
.liveExample { padding: 1em; background-color: #EEEEDD; border: 1px solid #CCC; max-width: 655px; }
.liveExample input { font-family: Arial; }
.liveExample b { font-weight: bold; }
.liveExample p { margin-top: 0.9em; margin-bottom: 0.9em; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class='liveExample'>
<p>Value: <input data-bind='value: value' /></p>
<p>Animated value: <input data-bind='value: value2' /></p>
<p>Animated value via animatedText binding: <span data-bind='animatedText: value'></span></p>
<p>Animated value via binding: <input data-bind='animatedValue: value' /></p>
<button data-bind="click: update">
+10
</button>
</div>
更新 1
我添加了 "animatedText" 自定义绑定处理程序。
ko.bindingHandlers.animatedText = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var value = valueAccessor();
var displayValue = ko.observable(value());
var timer;
value.subscribe(function(newValue) {
clearInterval(timer)
timer = setInterval(function() {
if(displayValue() == newValue) {
clearInterval(timer);
} else {
displayValue(displayValue() + 1);
ko.bindingHandlers.text.update(element, function() { return displayValue; });
}
}, 100);
});
ko.bindingHandlers.text.update(element, function() { return displayValue; });
}
};