Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a child element to drag a parent element

I want to make something like navigation bar that can move whole element, without allowing dragging anywhere on the parent entire element to control the dragging. For example:

<div class="undraggable">
    <div class="draggable"></div>
    content
</div>

Dragging on a title bar drags the whole window that it is contained within, but dragging elsewhere on the window container does not drag the window.

I tried to use dragresize but I can't make only draggable (don't want resizable) object with his code.

like image 408
hsgu Avatar asked May 21 '12 19:05

hsgu


3 Answers

I was looking for something like this but in jquery ui due to the fact I am already using it in my project so if you already using JQUERY UI you can identify the handle you wish to drag with. here is a working example: http://jsfiddle.net/VPMFs/

<style>
.undraggable{height:500px;width:500px;background:blue;}
.draggable{height:100px;width:100px;background:white;}
</style>
<div class="undraggable">
<div class="draggable">drag me!</div>
</div>

<script>
$('.undraggable').draggable({handle: '.draggable'});
</script>
like image 139
talsibony Avatar answered Nov 17 '22 13:11

talsibony


  1. Register your mousedown handler on the drag controller (e.g. the title bar of the window).
  2. When you are dragging, update the position of a different element (e.g. the window wrapper).

I have an example of this here:
http://phrogz.net/js/PhrogzWerkz/WerkWin.html

If you need this to work with a specific library—like a jQuery UI library—then please edit your question to say say.

Simpler Demo: http://jsfiddle.net/VCQuN/1/

<div class="window">
  <div class="titlebar">Hello, World!</div>
  <div class="content">
    <p>Window <b>Content!</b></p>
  </div>
</div>​
// For each item with a `window` class…
var windows = document.querySelectorAll('.window');
[].forEach.call(windows,function(win){

  // …find the title bar inside it and do something onmousedown
  var title = win.querySelector('.titlebar');
  title.addEventListener('mousedown',function(evt){

    // Record where the window started
    var real = window.getComputedStyle(win),
        winX = parseFloat(real.left),
        winY = parseFloat(real.top);

    // Record where the mouse started
    var mX = evt.clientX,
        mY = evt.clientY;

    // When moving anywhere on the page, drag the window
    // …until the mouse button comes up
    document.body.addEventListener('mousemove',drag,false);
    document.body.addEventListener('mouseup',function(){
      document.body.removeEventListener('mousemove',drag,false);
    },false);

    // Every time the mouse moves, we do the following 
    function drag(evt){
      // Add difference between where the mouse is now
      // versus where it was last to the original positions
      win.style.left = winX + evt.clientX-mX + 'px';
      win.style.top  = winY + evt.clientY-mY + 'px';
    };
  },false);
});
​
like image 5
Phrogz Avatar answered Nov 17 '22 13:11

Phrogz


Have an attribute draggable="true" in parent element and draggable="false" in all child elements. Now if you drag any of the child element or parent element , you drag will work properly. If you don't add draggable="false" in image tag, your drag and drop feature wont work properly. Drag and drop with nested elements

<span className="dragelement cp"  draggable={true} 
onDragStart={(event)=>drag(event)} onDragEnd={(event)=>dragEnd(event)} 
id= {'drag'+el.type+index+idx}>
    <div>{m.a}</div>  
    {m.img && <img draggable={false} src={m.img} height="70px" width="100px" />  } 
</span>
like image 2
Vishala Ramasamy Avatar answered Nov 17 '22 12:11

Vishala Ramasamy