jquery - 可拖动元素智能引用线无法正常工作

标签 jquery jquery-ui jquery-ui-draggable

我一直在寻找可拖动元素的智能指南,并在这里找到了一个不错的脚本 - https://stackoverflow.com/a/22062111

它工作得很好,直到我稍微改变了结构。 如果包装器对齐 display:inline-block 或 float:left ,则指南无法正确显示

我在这里创建了一个 fiddle - http://jsfiddle.net/A6CpP/110/

$.ui.plugin.add("draggable", "smartguides", {
    start: function(event, ui) {
        var i = $(this).data("draggable");
    console.log(i);
    var o = i.options;
        i.elements = [];
        $(o.smartguides.constructor != String ? ( o.smartguides.items || ':data(draggable)' ) : o.smartguides).each(function() {
            var $t = $(this); var $o = $t.offset();
            if(this != i.element[0]) i.elements.push({
                item: this,
                width: $t.outerWidth(), height: $t.outerHeight(),
                top: $o.top, left: $o.left
            });
        });
    },
    stop: function(event, ui) {
        $(".objectx").css({"display":"none"});
        $(".objecty").css({"display":"none"});
    },
    drag: function(event, ui) {
        var inst = $(this).data("draggable"), o = inst.options;
        var d = o.tolerance;
        $(".objectx").css({"display":"none"});
        $(".objecty").css({"display":"none"});
            var x1 = ui.offset.left, x2 = x1 + inst.helperProportions.width,
                y1 = ui.offset.top, y2 = y1 + inst.helperProportions.height,
                xc = (x1 + x2) / 2, yc = (y1 + y2) / 2;
            for (var i = inst.elements.length - 1; i >= 0; i--){
                var l = inst.elements[i].left, r = l + inst.elements[i].width,
                    t = inst.elements[i].top, b = t + inst.elements[i].height,
                    hc = (l + r) / 2, vc = (t + b) / 2;
                    var ls = Math.abs(l - x2) <= d;
                    var rs = Math.abs(r - x1) <= d;
                    var ts = Math.abs(t - y2) <= d;
                    var bs = Math.abs(b - y1) <= d;
                    var hs = Math.abs(hc - xc) <= d;
                    var vs = Math.abs(vc - yc) <= d; 
                if(ls) {
                    ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l - inst.helperProportions.width }).left - inst.margins.left;
                    $(".objectx").css({"left":l-d-4,"display":"block"});
                }
                if(rs) {
                    ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r }).left - inst.margins.left;
                     $(".objectx").css({"left":r-d-4,"display":"block"});
                }

                if(ts) {
                    ui.position.top = inst._convertPositionTo("relative", { top: t - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
                    $(".objecty").css({"top":t-d-4,"display":"block"});
                }
                if(bs) {
                    ui.position.top = inst._convertPositionTo("relative", { top: b, left: 0 }).top - inst.margins.top;
                    $(".objecty").css({"top":b-d-4,"display":"block"});
                }
                if(hs) {
                    ui.position.left = inst._convertPositionTo("relative", { top: 0, left: hc - inst.helperProportions.width/2 }).left - inst.margins.left;
                     $(".objectx").css({"left":hc-d-4,"display":"block"});
                }
                if(vs) {
                    ui.position.top = inst._convertPositionTo("relative", { top: vc - inst.helperProportions.height/2, left: 0 }).top - inst.margins.top;
                    $(".objecty").css({"top":vc-d-4,"display":"block"});
                }


            };
        }
});
$('.other-objects').draggable({
    containment: 'parent',
    smartguides:".other-objects",
    tolerance:5
});

最佳答案

您可以尝试其他插件。我为可拖动和可调整大小的插件添加了智能指南功能。

$('.drag')
    .draggable({
        smartGuides: '.drag:not(".selected")'
    })
    .resizable({
        handles: 'all',
        smartGuides: '.drag:not(".selected")'
    }

您可以看到它是如何工作的:

jsFiddle:https://jsfiddle.net/chukanov/bypr7Lt3/2/

github:https://github.com/aichukanov/smartguides

演示站点:https://ready2.net/smartguides.shtml

关于jquery - 可拖动元素智能引用线无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38560455/

相关文章:

javascript - 可移动的 jquery 框不应移出页面

jquery - 拖放 div 并存储位置

带有可拖动图像的 jQuery UI 图像轮播

javascript - 这个 jquery 循环有什么问题?

javascript - 是否有一个网站可以生成所有可能的单词显示方式?

javascript - 从左到右旋转同一个div

jquery-ui - 哪个输入失去焦点并获取值

javascript - 了解函数中 mouseenter 链接的类

javascript - 如何知道 HTMLTableDataCellElement 的整个文本被选中

javascript - 在特定差异/ margin 之后停止 jQueryUI Slider