jQuery UI sortable with body zoom不工作,不正确计算拖放区域

bwntbbo3  于 2023-10-17  发布在  jQuery
关注(0)|答案(2)|浏览(151)

我试图jQuery UI排序工作与缩放CSS属性的身体。但是它不允许我在正确的位置正确地拖放。

拖动只与半区工作,如果用户试图拖放从右侧的空白区域(黑色区域在我的情况下)它不工作

https://codepen.io/VijayDhanvai/pen/poqxEzQ

  • 红色区域,跌落不起作用 *
$(function () {
    var zoomScale = $("body").css("zoom");
    console.log(typeof zoomScale, zoomScale.toString());
    var canvasHeight = $("#sortable").height();
    var canvasWidth = $("#sortable").width();

    $("#sortable").sortable({
      sort: function (e, ui) {
        var changeLeft = ui.position.left - ui.originalPosition.left;
        var changeTop = ui.position.top - ui.originalPosition.top;
        var sortIndex =
          $("#sortable .ui-sortable-placeholder").index() + 30;
        var newLeft =
          ui.originalPosition.left +
          changeLeft / zoomScale -
          ui.item.parent().offset().left;

        var newTop =
          ui.originalPosition.top +
          changeTop / zoomScale -
          ui.item.parent().offset().top +
          sortIndex;

        ui.helper.css({
          left: newLeft,
          top: newTop,
        });
      },
    });
  });
$(function () {
        var zoomScale = $("body").css("zoom");
        console.log(typeof zoomScale, zoomScale.toString());
        var canvasHeight = $("#sortable").height();
        var canvasWidth = $("#sortable").width();

        $("#sortable").sortable({
          sort: function (e, ui) {
            var changeLeft = ui.position.left - ui.originalPosition.left;
            var changeTop = ui.position.top - ui.originalPosition.top;
            var sortIndex =
              $("#sortable .ui-sortable-placeholder").index() + 30;
            var newLeft =
              ui.originalPosition.left +
              changeLeft / zoomScale -
              ui.item.parent().offset().left;

            var newTop =
              ui.originalPosition.top +
              changeTop / zoomScale -
              ui.item.parent().offset().top +
              sortIndex;

            ui.helper.css({
              left: newLeft,
              top: newTop,
            });
          },
        });
      });
.ui-sortable-placeholder,
      .ui-sortable-helper {
        visibility: visible !important;
        background-color: black !important;
      }
      .ui-sortable-placeholder td,
      .ui-sortable-helper td {
        background-color: aqua;
      }

      table {
        /* width: 100%; */
        border-spacing: collapse;
        border-spacing: 0;
      }
      td {
        padding: 0px;
        min-width: 100%;
        border: solid 1px #ddd;
      }
      body {
        zoom: 1.3;

        
      }
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />    
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>
    <script type="text/javascript" src="https://github.com/borbit/jquery.viewport/raw/master/jquery.viewport.js"></script>
    <script type="text/javascript" src="https://github.com/borbit/jquery.scraggable/raw/master/jquery.scraggable.js"></script>

                  
  <table>
      <thead>
        <tr>
          <th>Month</th>
          <th>Savings</th>
        </tr>
      </thead>
      <tbody id="sortable">
        <tr>
          <td>January</td>
          <td>$100</td>
        </tr>
        <tr>
          <td>February</td>
          <td>$80</td>
        </tr>
        <tr>
          <td>January</td>
          <td>$100</td>
        </tr>
        <tr>
          <td>February</td>
          <td>$80</td>
        </tr>
        <tr>
          <td>January</td>
          <td>$100</td>
        </tr>
        <tr>
          <td>February</td>
          <td>$180</td>
        </tr>
      </tbody>
    </table>
qq24tv8q

qq24tv8q1#

可能原因

  • jQuery UI* sortable的行为是将鼠标位置与元素偏移量进行比较。但是,如果在父元素上设置zoom,似乎子元素的偏移值不会受到zoom的影响。(Safari CSS Reference稍微提到了它。

顺便说一下,根据Mozilla document of zoom,CSS zoom是非标准的。这意味着它有可能在不同的浏览器中有不同的行为。如果可能的话,尽量不要使用CSS zoom

解决方案:嵌套缩放

在您的例子中,如果<td>上设置了zoom,则可以很好地获取缩放后的宽度。因此,我尝试设置嵌套的zoom来模拟在<body>上设置zoom的视图:

tbody {
  zoom: calc(1 / 1.3);
}

td {
  zoom: 1.3;
  padding: 0px;
  min-width: 100%;
  border: solid 1px #ddd;
}

body {
  zoom: 1.3;
}

如果你这样做了,你就不必自己计算helper元素:

$("#sortable").sortable();

下面是demo:https://codepen.io/LyreneLiu/pen/PoXyQYx

i2byvkas

i2byvkas2#

而不是sortable使用可拖动ui.https://jqueryui.com/draggable/

$(document).ready(function(){
    $('td').addClass('tddrag');
});
$(function () {
    var zoomScale = $("body").css("zoom");
    console.log(typeof zoomScale, zoomScale.toString());
    var canvasHeight = $(".sort").height();
    var canvasWidth = $(".sort").width();

    $(".tddrag").draggable({
        sort: function (e, ui) {
            var changeLeft = ui.position.left - ui.originalPosition.left;
            var changeTop = ui.position.top - ui.originalPosition.top;
            var sortIndex =  $(".sort .ui-sortable-placeholder").index() + 30;
            var newLeft =  ui.originalPosition.left + ( changeLeft / zoomScale - ui.item.parent().offset().left);
            var newTop =  ui.originalPosition.top + (changeTop / zoomScale -  ui.item.parent().offset().top) +  sortIndex;
            ui.helper.css({  
                left: newLeft,
                top: newTop,
            });
        },
    });
});

相关问题