我有一个名为“workArea”的画布,它比视口大得多并且可拖动。我不认为这是相关的,但把它扔在那里。我正在使用下面的 init 函数动态创建带有淘汰赛的元素。它应该在最后一个元素的右侧和向下 25 像素处创建每个连续元素(技术上是选定的元素,但最后创建的元素会自动被选中)。locy 变量似乎按预期设置并递增,通过 25,但 locx 变量每次都呈指数级增长。我一定错过了一些愚蠢的东西。
Javascript:
ko.bindingHandlers.Item = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var $element = $(element);
var locx;
var locy;
var $pos = $('.itemView.focused').position();
var $work = $('#workArea');
if ($('.itemView.focused').length > 0) {
locx = $pos.left;
locy = $pos.top;
console.log('focused element "' + $('.itemView.focused').attr('name') + '" is at ' + locx + ',' + locy)
} else {
locx = $work.width() / 2;
locy = $work.height() / 2;
console.log('No focused elements, creating at ' + locx + ',' + locy)
}
$element.draggable({ ...draggable options here...});
locx += 25;
locy += 25;
$element.css('left', locx.toString() + 'px').css('top', locy.toString() + 'px');
console.log('Created new element at ' + locx + ',' + locy)
}
};
这是控制台日志,它与它们在 UI 上的显示位置相匹配
No focused elements, creating at 2000,1000
Created new element at 2025,1025
focused element "SERVER01" is at 2025,1025
Created new element at 2050,1050
focused element "SERVER02" is at 2298,1050
Created new element at 2323,1075
focused element "SERVER03" is at 2819,1075
Created new element at 2844,1100
focused element "APP01" is at 3588,1100
Created new element at 3613,1125