【问题标题】:Swapping with draggable and droppable is not working与可拖动和可放置交换不起作用
【发布时间】:2016-06-23 21:22:27
【问题描述】:

我正在使用 Jquery UI 来实现 DIV 交换。

问题:交换问题已解决,但 CSS 在交换期间出现问题。滚动条出现在底部,div 在交换期间也在改变形状。交换看起来也很不专业:(

谁能告诉我如何让它顺利交换?

例如here

<div class="container">
  <h1>Drag The Colored DIVs</h1>
  <div class="row">
    <div class="col-sm-4">
      <div class="drophere">
        <div class="draghere" style="background-color:yellow;">
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        </div>
      </div>
    </div>
    <div class="col-sm-4">
      <div class="drophere">
        <div class="draghere" style="background-color:pink;">
          2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
        </div>
      </div>
    </div>
    <div class="col-sm-4">
      <div class="drophere">
        <div class="draghere" style="background-color:yellow;">
          3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
        </div>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-sm-4">
      <div class="drophere">
        <div class="draghere" style="background-color:yellow;">
          4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 44 4 4 4 4 4 4 4 4 4 4 4 4 4 4 44 4 44 44
        </div>
      </div>
    </div>
    <div class="col-sm-4">
      <div class="drophere">
        <div class="draghere" style="background-color:pink;">
          5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
        </div>
      </div>
    </div>
    <div class="col-sm-4">
      <div class="drophere">
        <div class="draghere" style="background-color:yellow;">
          6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 66 6
        </div>
      </div>
    </div>
  </div>
</div>

CSS:

.draghere {
  margin:5px;
}

Javascript:

$(document).ready(function() {
  window.startPos = window.endPos = {};

  makeDraggable();

  $('.drophere').droppable({
    hoverClass: 'hoverClass',
    drop: function(event, ui) {
      var $from = $(ui.draggable),
        $fromParent = $from.parent(),
        $to = $(this).children(),
        $toParent = $(this);

      window.endPos = $to.offset();

      swap($from, $from.offset(), window.endPos, 20);
      swap($to, window.endPos, window.startPos, 500, function() {
        $toParent.html($from.css({
          position: 'relative',
          left: '',
          top: '',
          'z-index': ''
        }));
        $fromParent.html($to.css({
          position: 'relative',
          left: '',
          top: '',
          'z-index': ''
        }));
        makeDraggable();
      });
    }
  });

  function makeDraggable() {
    $('.draghere').draggable({
      zIndex: 99999,
      opacity: 0.35,
      revert: 'invalid',
      start: function(event, ui) {
        window.startPos = $(this).offset();
      }
    });
  }

  function swap($el, fromPos, toPos, duration, callback) {
    $el.css('position', 'absolute')
      .css(fromPos)
      .animate(toPos, duration, function() {
        if (callback) callback();
      });
  }
});

【问题讨论】:

    标签: javascript jquery html css jquery-ui


    【解决方案1】:

    你的代码对我来说似乎很好。您遇到的唯一小问题是您为交换指定的持续时间。如果您删除 20 和 500(指定的持续时间),它应该会更平滑地交换并且滚动条将不再出现。

    我将您的代码更新如下:

      swap($from, $from.offset(), window.endPos, 0);
          swap($to, window.endPos, window.startPos, 0, function() {
            $toParent.html($from.css({
              position: 'relative',
              left: '',
              top: '',
              'z-index': ''
            }));
    

    [更新]如果要保留动画:

    我根据您的代码在JsFiddle 创建了一个示例。

    为了解决您的问题,我必须控制 css。 .drophere 应该有在动画后重置形状的 css 规则。

    【讨论】:

    • 让我试试这个:)
    • 嘿 GH KARIM,我已经尝试过这种方式,这只是删除动画。我希望 CSS 动画 :(
    • @b0y 好的。我不明白这一点:-)。请查看更新的答案!
    • 嘿 GH Karim,我无法删除引导程序,该项目太大而无法删除它。我也尝试保持 FLOAT 但没有奏效(UI Disturbed):((
    • 你好@b0y。问题是在制作动画时列正在调整大小。但是,我现在设法更好地控制它。保持你的引导。希望这行得通!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多