jquery可排序占位符高度问题

发布于 2024-11-09 18:17:26 字数 74 浏览 4 评论 0原文

由于某种原因,我的可排序项目的占位符约为 10 像素。我所有可排序的物品都有不同的高度。如何更改每个占位符的高度以匹配正在移动的项目?

For some reason the placeholder for my sortable items is about 10px. All my sortable items have different heights. How can I change the height of each placeholder to match the item being moved?

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

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

发布评论

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

评论(6

诠释孤独 2024-11-16 18:17:26

我通常通过绑定一个回调来解决这个问题,该回调将占位符的高度设置为排序到 开始事件。这是一个简单的解决方案,可让您精细控制占位符的显示方式。

$( ".column" ).sortable({
    connectWith: ".column",
    start: function(e, ui){
        ui.placeholder.height(ui.item.height());
    }
});

请参阅更新的测试用例

I usually solve this by binding a callback that sets the height of the placeholder to the height of the item being sorted to the start event. It's a simple solution that gives you fine-grained control over how you want your placeholder to be displayed.

$( ".column" ).sortable({
    connectWith: ".column",
    start: function(e, ui){
        ui.placeholder.height(ui.item.height());
    }
});

See updated test case

以可爱出名 2024-11-16 18:17:26

您是否尝试过设置 forcePlaceholderSize:true 属性?请阅读 jQuery UI Sortable 文档页面

Have you tried setting the forcePlaceholderSize:true property? Have a read on the jQuery UI Sortable documentation page.

难忘№最初的完美 2024-11-16 18:17:26

当从外部容器拖动项目时,您也可以使用“ui.helper[0].scrollHeight”来稳定结果,而不是使用“ui.item.height()”。

$( ".column" ).sortable({
    connectWith: ".column",
    start: function(e, ui){
        ui.placeholder.height(ui.helper[0].scrollHeight);
    }
});

Instead of using "ui.item.height()" you can use "ui.helper[0].scrollHeight" to stable result when drag an item from external container too.

$( ".column" ).sortable({
    connectWith: ".column",
    start: function(e, ui){
        ui.placeholder.height(ui.helper[0].scrollHeight);
    }
});
于我来说 2024-11-16 18:17:26

你的元素是 display: block 吗?内联元素可能会使占位符无法正确保持高度。例如。 这个jsFiddle似乎与您描述的问题有类似的问题。将 display: block 添加到 .portlet 类可以修复此问题。

Are your elements display: block ? Inline elements might make the placeholder not keep the height correctly. Eg. this jsFiddle seems to have a similar problem to the one you described. Adding display: block to the .portlet class fixes it.

卷耳 2024-11-16 18:17:26

就我而言,我找到了类似于 JP Hellemons 的解决方案,其中我强制占位符的高度(带有!重要)自定义,并设置背景可见性以查看我自己的更改(您也可以按照您想要的方式设置占位符的样式)。

这也适用于 display: inline-block;

.ui-sortable-placeholder { 
     background:red !important; 
     height: 2px !important; // this is the key, set your own height, start with small
     visibility: visible !important;
     margin: 0 0 -10px 0; // additionaly, you can position your placeholder, 
     }                    // in my case it was bottom -10px

In my case I found solution similar to JP Hellemons, in which I'm forcing height of placeholder (with !important) to custom and also setting visibility with background to see changes for myself (also you can style this way your placeholder anyhow you want).

This works also with display: inline-block;

.ui-sortable-placeholder { 
     background:red !important; 
     height: 2px !important; // this is the key, set your own height, start with small
     visibility: visible !important;
     margin: 0 0 -10px 0; // additionaly, you can position your placeholder, 
     }                    // in my case it was bottom -10px
£冰雨忧蓝° 2024-11-16 18:17:26

您可以为占位符设置样式作为可排序的选项。

$( "#sortable" ).sortable({
        placeholder: "ui-state-highlight"
});

并给这种风格一个高度。希望有效。

You can set a style for your placeholder as an option for your sortable.

$( "#sortable" ).sortable({
        placeholder: "ui-state-highlight"
});

And just give that style a height. Hope that works.

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