在模态弹出窗口

时间:2016-02-10 12:09:14

标签: javascript jquery html twitter-bootstrap bootstrap-modal

我尝试使用draggingdropping实现div的交换。 没有模态弹出窗口它工作正常。

但是当我在模态弹出窗口中实现相同的操作时,拖放操作无法正常工作。

我错过了什么?

HTML

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
       <div class='droppable'>
    <div class="draggable">Draggable 1</div>
  </div>

  <div class='droppable'>
    <div class="draggable">Draggable 2</div>
  </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>    
  </div>
</div>

的javaScript

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

      makeDraggable();

      $('.droppable').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, 200);
          swap($to, window.endPos, window.startPos, 1000, function() {
            $toParent.html($from.css({position: 'relative', left: '', top: '', 'z-index': ''}));
            $fromParent.html($to.css({position: 'relative', left: '', top: '', 'z-index': ''}));
            makeDraggable();
          });
        }
      });

      function makeDraggable() {
        $('.draggable').draggable({
          zIndex: 99999,
          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();
          });
      }
    });

Draggable Demo

Draggable Demo inside modal popup

1 个答案:

答案 0 :(得分:0)

只是因为默认情况下的模态是g++所以javascript无法找到要初始化拖动功能的元素!

您需要做的是延迟拖动功能,直到元素不显示为无。

除了拖动工作之外,小提琴搞砸了,因为它缺少jQuery文件,也不要忘记优先级jQuery必须是第一个要加载的JS文件

https://jsfiddle.net/2eysmghe/2/