最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Use a child element to drag a parent element - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited May 21, 2012 at 19:52 Phrogz 303k113 gold badges667 silver badges756 bronze badges asked May 21, 2012 at 19:20 hsguhsgu 8644 gold badges9 silver badges20 bronze badges 1
  • See this answer stackoverflow.com/a/71743513/488802 which limits the parts of the parent are draggable – Ken Commented Apr 4, 2022 at 20:46
Add a comment  | 

4 Answers 4

Reset to default 8

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>
  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);
});
​

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.

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

use jQuery UI draggable feature:

http://jqueryui.com/demos/draggable/

you can build a custom download of jQuery UI to only get the components you need (draggable and it's dependencies) here: http://jqueryui.com/download.

It has a lot of configuration options - your wording is a little ambiguous but I think you may want the handle option: http://jqueryui.com/demos/draggable/#handle

发布评论

评论列表(0)

  1. 暂无评论