jQuery 可排序 - 连接问题

发布于 2024-11-09 15:24:41 字数 490 浏览 4 评论 0原文

我在 jsFiddle 上有两个示例:

示例 1

在第一个中,我可以移动列表元素,甚至从子元素到根元素。然而,要让它“啪”的一声,却并不是一件容易的事。

示例 2

在第二个示例中,我浮动了列表元素。这次我无法让它们“捕捉”到子元素。

问题

  • 有没有什么好方法可以让拍摄变得更容易?
  • 怎么用float来解决呢?

I have two examples on jsFiddle:

Example 1

In the first one I can move the list elements around, even from a child element to the root element. However, it's not very easy to make it "snap".

Example 2

In the second example I have floated the list elements. This time I can't get them to "snap" to the child elements.

Questions

  • Is there a good way to make it easier to snap?
  • How do I solve it with float?

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

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

发布评论

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

评论(2

愛上了 2024-11-16 15:24:41

jQuery sortable 中有一个迄今为止未记录的选项来定义检查容差的元素:toleranceElement。如果未设置,则会检查整个项目的位置(包括嵌套列表),但如果设置了,则仅考虑与选择器匹配的子项目。

这意味着您必须稍微更改 HTML 标记,以将列表项的主要内容(在您的情况下为项目文本)包装在元素中,并使用它来检查排序位置。这将省略嵌套的

    ,有效地阻止抖动,否则会非常严重。

HTML:

<div id="example5"> 
    <ul> 
        <li ><div>Item 1</div>
            <ul> 
                <li ><div>Item 1 1</div><ul></ul></li> 
                <li ><div>Item 1 2</div><ul></ul></li> 
                <li ><div>Item 1 3</div><ul></ul></li> 
            </ul> 
        </li> 
        <li ><div>Item 2</div><ul></ul></li> 
        <li ><div>Item 3</div><ul></ul></li> 
        <li ><div>Item 4</div><ul></ul></li> 
    </ul> 
</div> 

Javascript:

$("#example5 ul").sortable({
    connectWith: "#example5 ul",
    placeholder: "ui-state-highlight",
    toleranceElement: 'div'
});

尝试修改后的演示此处此处

您可能还想使用 opacitycursorAttolerance。查看文档

请注意,这不是一个完美的解决方案,如果您对结果不满意,请考虑使用插件。 Manuele J Sarfatti 的 nestedSortable 插件看起来可能对您有用:http:// /mjsarfatti.com/sandbox/nestedSortable/

There is a so far undocumented option in jQuery sortable to define the element against which tolerance is checked: toleranceElement. If not set, the whole item is checked for placement (including the nested list), but if it's set, only sub-items matching the selector are taken into account.

This means you have to change your HTML markup a bit to wrap the main content of the list item (the item text in your case) in an element and use that to check for sort positioning. That will leave out the nested <ul>s, effectively stopping jittering which is otherwise quite serious.

HTML:

<div id="example5"> 
    <ul> 
        <li ><div>Item 1</div>
            <ul> 
                <li ><div>Item 1 1</div><ul></ul></li> 
                <li ><div>Item 1 2</div><ul></ul></li> 
                <li ><div>Item 1 3</div><ul></ul></li> 
            </ul> 
        </li> 
        <li ><div>Item 2</div><ul></ul></li> 
        <li ><div>Item 3</div><ul></ul></li> 
        <li ><div>Item 4</div><ul></ul></li> 
    </ul> 
</div> 

Javascript:

$("#example5 ul").sortable({
    connectWith: "#example5 ul",
    placeholder: "ui-state-highlight",
    toleranceElement: 'div'
});

Try your modified demos here and here.

You might also want to play with opacity, cursorAt and tolerance. Have a look at the documentation.

Note that it's not a perfect solution, in case you're not satisfied with the results, have a look at using a plugin. Manuele J Sarfatti's nestedSortable plugin looks like something that might be of use for you: http://mjsarfatti.com/sandbox/nestedSortable/

十雾 2024-11-16 15:24:41

我正在 Safari 中查看您的两个示例,并且我能够将任何元素移入/移出子元素,而不会出现大问题。

这就是为什么 #2 更难使用的原因...

示例 #2 中的“放置区域”仅与您的元素一样高,因此对于较短的元素,可以让鼠标进入的区域要小得多。

示例 #1 中的“放置区域”非常宽,因此当您在列表中上下移动鼠标时很容易点击它。

我能想到的解决你的问题的唯一方法就是简单地让示例#2 中的项目更高,从而模拟示例#1 中除了侧面之外的项目。

http://jsfiddle.net/9uhfX/3/

I'm looking at both of your examples in Safari and I am able to move any element in/out of the children elements without a huge problem.

Here is why #2 is more difficult to use...

The "drop zone" in example #2 is only as high as your element so with your short elements, there is a much smaller area to get your mouse into.

The "drop zone" in example #1 is very wide so it's very easy to hit it as you move your mouse up/down the list.

The only way I can think of to solve your problem is to simply make your items in example #2 taller, thereby simulating what you have in example #1 except on its side.

http://jsfiddle.net/9uhfX/3/

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