jQuery UI sortable tolerance option not working as expected

前端 未结 6 1608
[愿得一人]
[愿得一人] 2020-12-14 04:13

Here is the part from jQuery UI documentation for the tolerance option:

This is the way the reordering behaves during drag. Possible valu

6条回答
  •  情深已故
    2020-12-14 04:44

    Here's an addition to @dioslaska's function. If you need to sort in a grid (e.g. your elements are floated), you can check the child element's top position to see if it's in the same "row" like so:

    sort: function (event, ui) {
      var self = $(this),
          width = ui.helper.outerWidth(),
          top = ui.helper.position().top;//changed to ;
    
      self.children().each(function () {
        if ($(this).hasClass('ui-sortable-helper') || $(this).hasClass('ui-sortable-placeholder')) {
          return true;
        }
        // If overlap is more than half of the dragged item
        var distance = Math.abs(ui.position.left - $(this).position().left),
            before = ui.position.left > $(this).position().left;
    
        if ((width - distance) > (width / 2) && (distance < width) && $(this).position().top === top) {
          if (before) {
            $('.ui-sortable-placeholder', self).insertBefore($(this));
          } else {
            $('.ui-sortable-placeholder', self).insertAfter($(this));
          }
          return false;
        }
      });
    }
    

提交回复
热议问题