gpt4 book ai didi

javascript - 拖动时将助手保持在鼠标下方

转载 作者:太空宇宙 更新时间:2023-11-04 13:20:10 25 4
gpt4 key购买 nike

我正在尝试实现与“可排序小部件”非常接近的功能,但由于其他不适用于预制小部件的功能,我无法使用它。所以我试图用可拖放元素重新创建它的功能:

$(".Element").draggable({
helper: 'original',
drag: function(event, ui) {

ElementWidth = $(this).outerWidth(true);
if($(this).prev().length){
LeftElementWidth = $(this).prev().outerWidth(true);
LeftElementLeftOffset = $(this).prev().offset().left;
if(parseFloat(ui.offset.left+(ElementWidth/2)) < parseFloat(LeftElementLeftOffset+(LeftElementWidth/2))){
$(this).prev().before($(this));
}
}

if($(this).next().length){
RightElementWidth = $(this).next().outerWidth(true);
RightElementLeftOffset = $(this).next().offset().left;
if(parseFloat(ui.offset.left+(ElementWidth/2)) > parseFloat(RightElementLeftOffset+(RightElementWidth/2))){
$(this).next().after($(this));
}
}
}
});

$("#Container").droppable({ accept: '.Element' });

它工作正常,除了当我将它的元素移动到下一个位置时 draggable-helper 不会停留在鼠标光标下方。看看这个 fiddle :

http://jsfiddle.net/5qFhg/15/

当您尝试对绿色框进行排序时,您会看到会发生什么。我怎样才能让助手保持原位?

最佳答案

http://jsfiddle.net/margaret_/XM6f8/1/

这是您要找的吗?您可以使用淘汰赛吗?我无法添加评论,因为我没有 50 的声誉。

<a href="#" data-bind="text: name, click: function() { viewModel.selectTask($data); },     visible: $data !== viewModel.selectedTask()"></a>
<input data-bind="value: name, visibleAndSelect: $data === viewModel.selectedTask(), event: { blur: function() { viewModel.selectTask(''); } }" />

使用父位置和之前的位置来模拟函数。

ko.bindingHandlers.sortableList = {
init: function(element, valueAccessor, allBindingsAccessor, context) {
$(element).data("sortList", valueAccessor()); //attach meta-data
$(element).sortable({
start: function(event, ui) {
//track the original position of the element
var parent = ui.item.parent();
var prev = ui.item.prev();
//create a function to move it back (if it has a prev sibling, insert after it, otherwise put it at the beginning)
ui.item.moveItemBack = prev.length ? function() { ui.item.insertAfter(prev); } : function() { parent.prepend(ui.item); };
},
update: function(event, ui) {
var item = ui.item.data("sortItem");
if (item) {
//identify parents
var originalParent = ui.item.data("parentList");
var newParent = ui.item.parent().data("sortList");

//figure out its new position
var position = ko.utils.arrayIndexOf(ui.item.parent().children(), ui.item[0]);

if (position >= 0) {
//move the element back to its original position and let KO handle adding it to the new parent
if (originalParent !== newParent) {
ui.item.moveItemBack();
}

//place item in the proper position
newParent.remove(item);
newParent.splice(position, 0, item);
}
}
},
connectWith: '.container'
});
}

您希望 div 并排显示吗?

关于javascript - 拖动时将助手保持在鼠标下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18717118/

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