开发者

JavaScript drag-and-drop proxy

I want to enable drag-an开发者_运维百科d-drop behaviour on my Web application. I have an image I want to drag. The image is behind another one that has transparent sections so that you can see the image beneath it. I do not want to change the order of the images. My thinking is I should use another layer on top of both images that is transparent and use it as a proxy to transfer events to the image I want to drag. jQuery UI's draggable function will not allow me to transfer the events in real-time i.e. I cannot hook into what it is doing while the drag is taking place, only when it is completed.

Is there a JavaScript library or jQuery plugin that will allow me to enable drag-and-drop on an element and have it transfer those events to another element in real-time?


Maybe I don't understand what you are trying to accomplish, but you should be able to drag and drop overlapping images without any trouble (demo).

Just wrap both images in a div and then make the div draggable:

CSS (no need to make .dragme position relative, because it is done in the draggable script)

.dragme img {
 position: absolute;
 top: 0;
 left: 0;
}

HTML

<div class="dragme">
 <img src="image1.gif">
 <img src="image2.gif">
</div>

Script

$(".dragme").draggable();

I updated the demo, this isn't pretty and there might be a better way, but basically this puts an invisible overlay over the frame, then positions the image while the overlay is being dragged.

CSS

#draggable, #droppable {
 width: 450px;
 height: 250px;
 padding: 0.5em;
 margin: 10px;
 background: #ddd;
 color:#000;
}
.dragme {
 position: relative;
 width: 100px;
 padding: 5px;
}
.dragme img {
 position: absolute;
 top: 55px;
 left: 30px;
}
.demo {
 width: 500px;
}
.border {
 position: absolute;
 top: 75px;
 left: 30px;
 z-index: 1;
}

HTML

<div class="demo">
 <div class="border">
  <img src="http://www.imageuploading.net/image/thumbs/large/border-564.png">
 </div>
 <div id="draggable" class="ui-widget-content">
  <p>Drag from here</p>   
  <div class="dragme">
   <img src="http://i142.photobucket.com/albums/r117/SaltyDonut/Icons/evilpuppy.gif">
  </div>
 </div>
 <div id="droppable">
  <p>Drop here</p>
 </div>
</div>

Script (The demo uses $(document).ready because jsFiddle doesn't like $(window).load)

$(window).load(function(){
 // cycle through draggable divs (in case there are more than one)
 $(".dragme").each(function(){
  var img = $(this).find('img');
  var pos = img.position();
  // create div overlay on image
  $('<div/>', {
   class : 'overlay',
   css: {
    position: 'relative',
    top: pos.top,
    left: pos.left,
    width: img.outerWidth(),
    height: img.outerHeight(),
    zIndex: 100
   }
  })
   // save original image position
   .data('pos', [pos.left, pos.top])
   .appendTo($(this));

  // make overlay draggable
  $(this).find('.overlay').draggable({
   containment : '.demo',
   revert: true,
   revertDuration: 0,
   handle: 'div',
   // drag overlay and image
   drag: function(e,ui){
    img = $(this).parent().find('img');
    img.css({
     top: ui.position.top,
     left: ui.position.left
    });
   },
   // make image revert
   stop: function(e,ui){
    pos = $(this).data('pos');
    $(this).parent().find('img').animate({left: pos[0], top: pos[1] },500);
   }
  });
 });

 $("#droppable").droppable({
  drop : function(e,ui) {
   // append entire div wrapper (.dragme)
   ui.helper.parent().appendTo($(this));
  }
 });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜