1
我有一个名为“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
尝试使用'$ pos = $('。itemView.focused')。position()'和'$ work = $('#workArea')':这会改变什么吗? – MaxArt
相同的行为:( –
更新的代码,以反映这种变化,以防万一 –