拖动时在鼠标下保持帮助

时间:2013-09-10 10:59:14

标签: javascript jquery html css jquery-ui

我正在尝试实现一些非常接近'Sortable Widget'的东西,尽管由于其他与premade 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' });

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

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

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

1 个答案:

答案 0 :(得分:0)

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'
    });
}

你想让divs并排出现吗?