如何避免将宽度和高度添加到拖动项目中?

发布于 2025-02-02 03:09:03 字数 1019 浏览 3 评论 0原文

当我使用可拖动且可排序时,jQuery总是设置内联风格的宽度和高度,这确实很烦人。

这是我将项目从.structure转移到.sortable的脚本:

$( function()
{
    $( ".sortable" ).sortable();

    $( ".item" ).draggable({
        connectToSortable: ".sortable",
        helper: "clone"
    });
});

这是我的HTML代码

<div class="structure">
    <div class="item">New block</div>
    <div class="item">New headline</div>
</div>

<div class="sortable"></div>

.Item是块元素,当我将它们移至。可容纳它们获得宽度和高度,而不是以宽度:100%传播。这就是.Item拖动后的样子:

<div class="item ui-draggable ui-draggable-handle" style="width: 69.75px; height: 18px;">New block</div>

在这里您可以看到 https://codepen.io/alphafrau/alphafrau/pen/nwyxmgw

这些设置以及如何避免这种情况。你有什么想法吗?

亲切的问候 彼得

When I use draggable and sortable, jQuery always sets inline-styles for width and height which is really annoying.

This is my script to move items from .structure to .sortable:

$( function()
{
    $( ".sortable" ).sortable();

    $( ".item" ).draggable({
        connectToSortable: ".sortable",
        helper: "clone"
    });
});

and this is my HTML code

<div class="structure">
    <div class="item">New block</div>
    <div class="item">New headline</div>
</div>

<div class="sortable"></div>

The .item are block elements, when I move them to .sortable they get a width and height instead of spreading at a width: 100%. This is what a .item looks like after dragging:

<div class="item ui-draggable ui-draggable-handle" style="width: 69.75px; height: 18px;">New block</div>

Here you can see that
https://codepen.io/alphafrau/pen/NWyXmgw

I have no idea why jQuery adds these settings and how to avoid that. Do you have any ideas?

Kind regards
Peter

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

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

发布评论

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

评论(2

人事已非 2025-02-09 03:09:03

$(this)元素“可排序事件”所指的是指橙色div,其中所有孩子divs中的所有孩子都必须样式。

$(this).children().each(function () {
              $(this).css("width", "100%");
            });

下面的代码中有console.log的事件,以便您意识到发生了什么。

     $(".sortable").sortable({
        start: function () {
          console.log("start sorting");
        },
        stop: function (e) {
          console.log("stop sorting");
          $(this)
            .children()
            .each(function (index) {
              $(this).css("width", "100%");
            });
        },
      });

      $(".item").draggable({
        connectToSortable: ".sortable",
        helper: "clone",
        start: function () {
          //console.log("start drag");
        },
        drag: function () {
          //console.log("dragging");
        },
        stop: function (e) {
          //console.log("stop drag");
        },
      });
      .item {
        background: lime;
        border: 2px solid green;
        padding: 5px;
        margin: 2px;
        width: 100%;
      }

      .sortable {
        background: Orange;
        border: 2px solid OrangeRed;
        padding: 20px;
        margin: 0 0 0 350px;
      }

      .sortable .item1 {
        background: fuchsia;
        border: 2px solid brown;
        padding: 5px;
        margin: 2px;
      }
      .ui-draggable-dragging {
        /*while being dragged*/
        background: hwb(305 0% 0%);
      }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
<div class="structure">
      <div class="item">New block</div>
      <div class="item">New headline</div>
    </div>

    <div class="sortable"></div>

The $(this) element the sortable events act on refers to the orange div with all the child divs in it, you have to style them.

$(this).children().each(function () {
              $(this).css("width", "100%");
            });

There are console.log's and events in the code below for you to realize what's going on.

     $(".sortable").sortable({
        start: function () {
          console.log("start sorting");
        },
        stop: function (e) {
          console.log("stop sorting");
          $(this)
            .children()
            .each(function (index) {
              $(this).css("width", "100%");
            });
        },
      });

      $(".item").draggable({
        connectToSortable: ".sortable",
        helper: "clone",
        start: function () {
          //console.log("start drag");
        },
        drag: function () {
          //console.log("dragging");
        },
        stop: function (e) {
          //console.log("stop drag");
        },
      });
      .item {
        background: lime;
        border: 2px solid green;
        padding: 5px;
        margin: 2px;
        width: 100%;
      }

      .sortable {
        background: Orange;
        border: 2px solid OrangeRed;
        padding: 20px;
        margin: 0 0 0 350px;
      }

      .sortable .item1 {
        background: fuchsia;
        border: 2px solid brown;
        padding: 5px;
        margin: 2px;
      }
      .ui-draggable-dragging {
        /*while being dragged*/
        background: hwb(305 0% 0%);
      }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
<div class="structure">
      <div class="item">New block</div>
      <div class="item">New headline</div>
    </div>

    <div class="sortable"></div>

烙印 2025-02-09 03:09:03

使用宽度:100%!重要它可能会为您提供帮助。

Use width:100% !important It may help you.

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