将助手保持在鼠标拖动时的位置

3

我正在尝试实现与“Sortable Widget”非常接近的功能,但由于其他事情与预制小部件不兼容,因此无法使用它。因此,我正在尝试使用可拖动和可放置元素重新创建其功能:

$(".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' });

除了拖动助手不在鼠标光标下保持位置之外,它的功能很好。当我将其元素移动到下一个位置时,请查看此fiddle:

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

您将看到在尝试排序绿色框时会发生什么。如何保持助手的位置?


你说你不想要“可排序小部件”,但你决定使用“可拖动”和“可放置”元素...为什么不使用带有一些自定义行为的“可排序”元素呢?jQuery UI:Sortable - Cholesterol
是什么阻止你使用可排序小部件?此外,您是否希望能够垂直移动这些框并将它们移动到红框之外?您是否希望它们在鼠标松开时自动吸附到空白区域? - olleicua
1个回答

0

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

这是您要找的吗?您是否愿意使用knockout?我不能添加评论,因为我没有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 并排显示吗?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接