jquery 可拖动包含:表的子集

发布于 2024-09-16 10:59:36 字数 36 浏览 4 评论 0原文

我怎样才能使收容成为细胞的子集。即省略标题行和最左边的列?

how can i make the containment to be a subset of cells. ie leaving out the header row and the leftmost column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪 2024-09-23 10:59:36

最佳方案是只发送一个选择器来选择应构成遏制的字段,但由于这行不通,所以我摆弄了一个替代解决方案。我不会说这是非常理想的,但我会尝试一下。

找到您应该拖动可拖动项的单元格的极值点(左、上、右、下),并将这些点设置为可拖动项的包含范围。在我的示例中,我为这些元素(表的 td)指定了类 containmentTD。它没有考虑边框、填充等,因此如果这很重要,则需要对其进行调整。

//The TD elements that are to be included when
//we find the dimentions of the containment
var td_elements = $(".containmentTD");

//This will hold the extreme points of the containment
var points = {
    left: td_elements.eq(0).position().left,
    top: td_elements.eq(0).position().top,
    right: 0,
    bottom: 0
};

//Find the points of the containment
td_elements.each(function() {
    var t = $(this);
    var p = t.position();
    var width = t.width();
    var height = t.height();

    points.left = Math.min(p.left, points.left);
    points.top  = Math.min(p.top , points.top );
    points.right = Math.max( points.right, p.left + width);
    points.bottom = Math.max( points.bottom, p.top + height);
});

//This will only work "perfectly" when all draggables have
//the same dimentions
points.bottom -= $("div.draggable:first").height();
points.right  -= $("div.draggable:first").width();

$("div.draggable").draggable({
    containment: [points.left, points.top, points.right, points.bottom]
});

这是一个演示: http://jsfiddle.net/uTyTY/ 请注意,红色方块仅用于可视化收容区

The optimal would be to just send in a selector for the fields that should consitute the containment, but since that won't work I fiddled with an alternative solution. I wouldn't say it's very optimal but I'll give it a go.

Find the extreme points (left, top, right, bottom) of the cells that you are supposed to drag the draggable withing and set these points as the containment of the draggable. In my example I've given these elements (the tds of the table) the class containmentTD. It doesn't take into account borders, padding ect so it'll need to be tweaked if that's important.

//The TD elements that are to be included when
//we find the dimentions of the containment
var td_elements = $(".containmentTD");

//This will hold the extreme points of the containment
var points = {
    left: td_elements.eq(0).position().left,
    top: td_elements.eq(0).position().top,
    right: 0,
    bottom: 0
};

//Find the points of the containment
td_elements.each(function() {
    var t = $(this);
    var p = t.position();
    var width = t.width();
    var height = t.height();

    points.left = Math.min(p.left, points.left);
    points.top  = Math.min(p.top , points.top );
    points.right = Math.max( points.right, p.left + width);
    points.bottom = Math.max( points.bottom, p.top + height);
});

//This will only work "perfectly" when all draggables have
//the same dimentions
points.bottom -= $("div.draggable:first").height();
points.right  -= $("div.draggable:first").width();

$("div.draggable").draggable({
    containment: [points.left, points.top, points.right, points.bottom]
});

Here's a demo: http://jsfiddle.net/uTyTY/ Note that the red square is only there to visualize the containment area

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文