Skip to content

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>

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.



  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:

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:

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

  // …find the title bar inside it and do something onmousedown
  var title = win.querySelector('.titlebar');

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

    // 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

    // 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 = winX + evt.clientX-mX + 'px';  = winY + evt.clientY-mY + 'px';