使用子元素拖动父元素

时间:2012-05-21 19:20:32

标签: javascript draggable

我想制作一些可以移动整个元素的导航栏,而不允许拖动父整个元素上的任何位置来控制拖动。例如:

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

在标题栏上拖动会拖动其中包含的整个窗口,但拖动窗口容器上的其他位置不会拖动窗口。

我尝试使用dragresize,但我不能只用他的代码制作可拖动的(不想要可调整大小的)对象。

4 个答案:

答案 0 :(得分:6)

我正在寻找类似这样的东西,但在jquery ui中由于我已经在我的项目中使用它,所以如果你已经使用了JQUERY UI,你可以识别你想要拖动的句柄。 这是一个有效的例子: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 :(得分:3)

  1. 在拖动控制器上注册您的mousedown处理程序(例如窗口的标题栏)。
  2. 拖动时,更新其他元素的位置(例如窗口包装器)。
  3. 我在这里有一个例子:
    http://phrogz.net/js/PhrogzWerkz/WerkWin.html

    如果您需要使用特定的库(如jQuery UI库),那么请编辑您的问题说出来。

    更简单的演示: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);
    });
    ​
    

答案 2 :(得分:0)

使用jQuery UI可拖动功能:

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

您可以构建一个jQuery UI的自定义下载,只在这里获取您需要的组件(可拖动及其依赖项):http://jqueryui.com/download

它有很多配置选项 - 你的措辞有点含糊不清但我想你可能需要句柄选项:http://jqueryui.com/demos/draggable/#handle

答案 3 :(得分:0)

在父元素中有一个属性draggable="true",在所有子元素中有一个draggable="false"。现在,如果您拖动任何子元素或父元素,您的拖动将正常工作。 如果您不在图像标签中添加 draggable="false",您的拖放功能将无法正常工作。 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>
相关问题