gpt4 book ai didi

javascript - 仅在 slider 停止后更新 slider

转载 作者:行者123 更新时间:2023-11-30 00:14:36 26 4
gpt4 key购买 nike

我有一个 knockout.js slider 使用 jquery-ui slider .

<input id="ex2" data-slider-id="ex2Slider" type="text" data-bind="sliderValue: {value: borrowOrInvestAmount, min:0, max: 5000, step: 1, formatter:formatter2}, event: { change: $root.getInvestmentDetailsForBorrower }, valueUpdate:['afterkeydown','propertychange','input']" style="display: none;">

所以我尝试使用 value-update 但没有成功。现在,当我单击 slider 点而不拖动它时,它可以正常工作并调用 $root.getInvestment,但我希望在 slider 停止拖动后立即调用它。

现在,当我拖动 slider 时,它会停在所有点上,直到到达目标。

最佳答案

你只需要替换:

ko.utils.registerEventHandler(element, "slidestop",..)

ko.utils.registerEventHandler(element, "slidestop",...)

ko.utils.registerEventHandler(element, "slide", function (event, ui) {...

ko.utils.registerEventHandler(element, "stop", function (event, ui) {...

此更改会将发生更改的事件从 slide 更改为 stop

工作示例:

ko.bindingHandlers.slider = {
init: function (element, valueAccessor, allBindingsAccessor) {
var options = allBindingsAccessor().sliderOptions || {};
$(element).slider(options);
ko.utils.registerEventHandler(element, "slidestop", function (event, ui) {
var observable = valueAccessor();
observable(ui.value);
});
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$(element).slider("destroy");
});
ko.utils.registerEventHandler(element, "stop", function (event, ui) {
var observable = valueAccessor();
observable(ui.value);
});
},
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
if (isNaN(value)) value = 0;
$(element).slider("value", value);
}
};


var ViewModel = function() {
var self = this;

self.savings = ko.observable(10);
self.spent = ko.observable(5);
self.net = ko.computed(function() {
return self.savings() - self.spent();
});
}

ko.applyBindings(new ViewModel());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js"></script>

<h2>Slider Demo</h2>

Savings: <input data-bind="value: savings, valueUpdate: 'afterkeydown'" />
<div style="margin: 10px" data-bind="slider: savings, sliderOptions: {min: 0, max: 100, range: 'min', step: 1}"></div>

Spent: <input data-bind="value: spent, valueUpdate: 'afterkeydown'" />
<div style="margin: 10px" data-bind="slider: spent, sliderOptions: {min: 0, max: 100, range: 'min', step: 1}"></div>

Net: <span data-bind="text: net"></span>

关于javascript - 仅在 slider 停止后更新 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35196368/

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