jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用数组的jQuery draggable containment无法按预期工作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个 HTML
<div id="workflowEditor" class="workflow-editor">

    <div class="node" class="ui-widget" style="top:20px; left:40px;">
        <div class="ui-widget ui-widget-header ui-state-default">Header test</div>
        <div class="ui-widget ui-widget-content">Test Content</div>
    </div>

</div>

有了这个CSS

.workflow-editor
{
    position: relative;
    overflow: auto;
}

.workflow-editor .node
{
    position: absolute;

    min-width: 64px;
    min-height: 64px;
}

比打电话

$(".node","#workflowEditor").draggable({
    containment: [0,500,500]
});

但是,拖动此“节点”将允许拖动到负值;似乎draggable正在使用浏览器的视口坐标来限制边界框.有没有办法告诉坐标是否相对于可拖动的父母?

注意:父母可能会随着时间的推移改变位置.

**编辑**

可拖动物所在的表面相对于其他一些内容.就像CSS指定的那样,我需要它溢出:滚动;,所以如果拖动拖动到外面,那么将显示滚动条.父母的大小是固定的.我遇到的问题是拖拽可以拖到表面的左侧(或顶部)(因此我会失去它们).我想有一个类似的行为,如将包含设置为“父”,但允许draggables溢出到父的右/底侧.

解决方法@H_607_22@
计算节点及其父节点的边界,请检查 jsFiddle Link
根据 Jquery UI的解释
var containmentX1 = $(".node").parent().offset().left;
var containmentY1 = $(".node").parent().offset().top;
var containmentX2 =  ($(".node").parent().outerWidth() +  $(".node").parent().offset().left - $('.node').outerWidth())
var containmentY2 = ($(".node").parent().outerHeight() +  $(".node").parent().offset().top - $('.node').outerHeight()) 

$(".node").draggable({
    containment:  [containmentX1,containmentY1,containmentX2,containmentY2]
});

大佬总结

以上是大佬教程为你收集整理的使用数组的jQuery draggable containment无法按预期工作全部内容,希望文章能够帮你解决使用数组的jQuery draggable containment无法按预期工作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。