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 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>
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.
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With