使用jQuery-UI的简单可拖动DIV

时间:2013-10-01 10:10:41

标签: jquery html css jquery-ui

我想让div innerDropzone可以拖放下面的代码:

 div.example {
   width: 560px;
   height: 750px;
   display: block;
   padding: 10px 20px;
   color: black;
   background: rgba(255, 255, 255, 0.4);
   -webkit-border-radius: 8px;
   -khtml-border-radius: 8px;
   -moz-border-radius: 8px;
   border-radius: 8px;
   margin-bottom: 10px;
   border: 1px solid rgba(0, 0, 0, 0.2);
   position: relative;
   float: left;
   margin-right: 40px;
 }
 #dropzone {
   height: 530px;
   width: 530px;
   -webkit-border-radius: 10px;
   -khtml-border-radius: 10px;
   -moz-border-radius: 10px;
   border-radius: 10px;
   border: 2px dashed #0687FF;
   display: block;
   background-image: url(/Images/RE/02.png);
 }
 #imgzone {
   height: 150px;
   width: 150px;
   overflow: auto;
   -webkit-border-radius: 10px;
   -khtml-border-radius: 10px;
   -moz-border-radius: 10px;
   border-radius: 10px;
   border: 2px groove #0687FF;
   float: left;
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>

<div class="example">
  <div id="dropzone" style="text-align: center; float: left">
    <div id="innerDropzone" style="position: absolute; top: 20px; left: 30px; width: 250px; height: 250px">
      <img style="display: none;" id="img" src="nothing" />
    </div>
    <div id="hint" style="position: absolute; top: 22%; left: 30%;">Drop in images from your desktop</div>
  </div>
  <div id="imgzone">
    <div id="imagelist">
      <img id="sampleImg" src="/Images/RE/02.png" width="100px" height="100px" style="margin: 10px;" />
    </div>
  </div>
</div>

我尝试将DIV拖放如下,但失败了:

$('#innerDropzone').draggable();

6 个答案:

答案 0 :(得分:1)

如果你只需要使div可拖动,请尝试在jQuery中使用我的脚本,也可以使用触摸设备。

;(function ($, window, document, undefined) {
var $canvas = $('#canvas'),
    canvT = $canvas.offset().top,
    canvL = $canvas.offset().left,
    $elem = $('<div/>', {
      'class': 'ball'
    }),
    offL = 0,
    offT = 0,
    dragging = 0,
    touchDev = 0;
var onDrop = function onDrop(e) {
  $(document).off('mousemove.dp');
  $(document).off('mouseup.dp');
  dragging = 0;
};
var onDrag = function onDrag(e) {
  $elem.css({
    'left': e.pageX - canvL + offL,
    'top': e.pageY - canvT + offT
  });
};
$elem.appendTo($canvas);
if ('touchstart' in window) {
  touchDev = 1;
}
//touchDev = 1;
if (touchDev === 0) {
  console.log('mousedown');
  $elem.on('mousedown', function (e) {
    if (dragging === 0) {
      offL = $(this).offset().left - e.pageX;
      offT = $(this).offset().top - e.pageY;
    }
    dragging = 1;
    $(document).on('mousemove.dp', onDrag);
    $(document).on('mouseup.dp', onDrop);
  });
} else {
  $elem.on('touchstart', function(event) {
    var e = event.originalEvent;
    var touch = e.targetTouches[0];
    offL = $(this).offset().left - touch.pageX;
    offT = $(this).offset().top - touch.pageY;
  });
  $elem.on('touchmove', function(event) {
    var e = event.originalEvent,
        touch;
    if (e.targetTouches.length == 1) {
      touch = e.targetTouches[0];
      onDrag(touch);
    }
  });
}
})(jQuery, window, document);

CSS

* {
  margin: 0;
  padding: 0;
}
#canvas {
  position: relative;
  top: 20px;
  left: 20px;
  width: 300px;
  height: 300px;
  border: 1px solid;
}
.ball {
  position: absolute;
  width: 80px;
  height: 80px;
  background-color: green;
  border-radius: 9999px;
}

JS Bin http://output.jsbin.com/joweca/

答案 1 :(得分:1)

对于Jquery-ui版本1.8.23与jquery 1.9.1不兼容。 (根据我的经验)

然而,我发现on this Wiki link Jquery-ui 1.8.23与Jquery 1.3.2 +兼容。

我用最新的库测试你的html,一切正常。here is fiddle

因此它是Jquery UI和Jquery库兼容性问题。使用最新或兼容的库。

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

答案 2 :(得分:0)

查看此网站http://jqueryui.com/draggable/它有一个指南,可以执行您要执行的操作。还要确保在您的示例中引用了jQuery库

答案 3 :(得分:0)

 <img style="display: none;" id="img" src="nothing"/>

当图像里面没有显示时,你怎么能看到元素,给它样式

#innerDropzone { width: 150px; height: 150px; padding: 0.5em; border:1px solid #000; }

可拖动,请检查here

答案 4 :(得分:0)

看一下这个网站上的例子,它显示了使用jquery拖放的非常简单的代码。 http://www.overpie.com/jquery/articles/jquery-drag-and-drop-example

答案 5 :(得分:0)

我的代码中没什么大不了的。看来你在head标签中使用了JQuery $('#innerDropzone').draggable();
只需将其包裹在这样的功能中:

$(function () {
    $('#innerDropzone').draggable();
});