gpt4 book ai didi

javascript - 可观察到的 knockout 是否可以实现数字动画行为?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:13:42 25 4
gpt4 key购买 nike

例如你有这样的简单模型:

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 中)。在这个示例中,这完成了抛出新变量和自制计时器。

问题

这里有什么方法可以通过自定义绑定(bind)或自定义扩展器来完成此类行为吗?

最佳答案

我已经绘制了自定义“animatedValue”绑定(bind)的草图,希望对您有所帮助:

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”自定义绑定(bind)处理程序。

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; });
}
};

关于javascript - 可观察到的 knockout 是否可以实现数字动画行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37876343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com