8

如果有人可以帮助我弄清楚如何使 div 中包含的可拖动元素根据窗口大小改变比例,我将非常感谢任何指导。

如果我做:

element.draggable({ 
    cursor: "move",
    containment: '#container'
});

会发生什么是它给了我容器的常规大小的容器。因此,如果我有一个transform: scale(1.5),容器中将有可拖动元素无法进入的空间。

我也尝试过containment: 'parent',但这很容易出错。

编辑

我已经找到了如何获得顶部和左侧的遏制,但我不知道如何获得右侧和底部。

var containmentArea = $("#container");

containment:  [containmentArea.offset().left, containmentArea.offset().top, ???, ???]

我已经尝试过宽度高度containmentArea[0].getBoundingClientRect()但这似乎也不是正确的举动。


这是一些示例代码的jsfiddle。

4

3 回答 3

6

在拖动事件中重置坐标的版本(因为它们已经被重新计算以进行比例转换),而不使用包含:

var percent = 1, containmentArea = $("#container");

function dragFix(event, ui) {
    var contWidth = containmentArea.width(), contHeight = containmentArea.height();
    ui.position.left = Math.max(0, Math.min(ui.position.left / percent , contWidth - ui.helper.width()));
    ui.position.top = Math.max(0, Math.min(ui.position.top  / percent,  contHeight- ui.helper.height()));
}

$(".draggable").draggable({
    cursor: "move",
    drag: dragFix,
});

//scaling here (where the percent variable is set too)

小提琴

在示例中容器的宽度和高度是在拖动事件中获得的,您也可以在缩放时存储它们以获得更好的性能。通过在事件中计算它们,它们在重新缩放后仍然可以工作,尽管仍然必须设置百分比变量。为了真正通用,它也可以在事件内部获得(并且可以使用 ui.helper.parent() 代替固定容器)因为拖动事件内部的偏移量是(0,0)与容器相关(至少它适用于当前设置),冒昧地简化originalleft + (position - originalposition)/percentposition / percent Start offset 似乎不再是必要的,所以把它放在小提琴中,但如果需要可以重新添加。

于 2015-07-13T14:58:44.760 回答
0

看看这个:

http://jsfiddle.net/z0gqy9w2/3/

编辑后的代码如下:

    // Matrix regex to take the scale value property of $('#container') element    
    var matrixRegex = /matrix\((-?\d*\.?\d+),\s*0,\s*0,\s*(-?\d*\.?\d+),\s*0,\s*0\)/,
    matches = $('#container').css('transform').match(matrixRegex);
    // Matches have this value : ["matrix(1.5, 0, 0, 1.5, 0, 0)", "1.5", "1.5"] , so we need matches[1] value :
    var scaleValue = matches[1];
    $(".draggable").draggable({
        cursor: "move",
        start: startFix,
        drag: dragFix,
        containment: [containmentArea.offset().left, containmentArea.offset().top, 
                      ( ( containmentArea.offset().left + ( containmentArea.width() * scaleValue ) ) - ( $(".draggable").width() * scaleValue ) ) ,
                      ( ( containmentArea.offset().top + ( containmentArea.height() * scaleValue ) ) - ( $(".draggable").height()  * scaleValue ) ) ]

    });

如您所见,这是诀窍:

( ( containmentArea.offset().left + ( containmentArea.width() * scaleValue ) ) - ( $(".draggable").width() * scaleValue ) )

您的右最大位置将是:主容器左偏移 + 容器的真实宽度(带有比例)- 项目真实宽度(让它在容器内)。

(提示:可以随意更改“百分比” var 值,也可以查看结果)

正则表达式参考

于 2015-07-12T19:00:49.063 回答
0

这是我的解决方案:

var _zoom = 1.2,
    $element = $('.draggable-element'),
    $container = $('#container');

var containmentW,
    containmentH,
    objW,
    objH;

$element.draggable({

    start: function(evt, ui) {
        ui.position.left = 0;
        ui.position.top = 0;

        containmentW = $container.width() * _zoom;
        containmentH = $container.height() * _zoom;
        objW = $(this).outerWidth() * _zoom;
        objH = $(this).outerHeight() * _zoom;

    },

    drag: function(evt, ui) {

        var boundReached = false,

            changeLeft = ui.position.left - ui.originalPosition.left,
            newLeft = ui.originalPosition.left + changeLeft / _zoom,

            changeTop = ui.position.top - ui.originalPosition.top,
            newTop = ui.originalPosition.top + changeTop / _zoom;


        // right bound check
        if(ui.position.left > containmentW - objW) {
            newLeft = (containmentW - objW) / _zoom;
            boundReached = true;
        }

        // left bound check
        if(newLeft < 0) {
            newLeft = 0;
            boundReached = true;
        }

        // bottom bound check
        if(ui.position.top > containmentH - objH) {
            newTop = (containmentH - objH) / _zoom;
            boundReached = true;
        }

        // top bound check
        if(newTop < 0) {
            newTop = 0;
            boundReached = true;
        }

        // fix position
        ui.position.left = newLeft;
        ui.position.top = newTop;

        // inside bounds
        if(!boundReached) {

            // do stuff when element is dragged inside bounds

        }

    }
});

链接到小提琴

于 2016-07-14T15:38:23.813 回答