Based on what was suggested earlier in my other question, I'm using a modified version of the suggestion to animate a drag movement. I'm aware the dragging part can be done using the html5 draggable
attribute, but it has many limitations and side effects which I'm trying to avoid.
(function() {
let targets = document.querySelectorAll('.draggable');
let offsetX;
let offsetY;
Array.prototype.map.call(targets, (target) => {
target.isMouseDown = false;
target.initialOffsetLeft = target.offsetLeft;
target.initialOffsetTop = target.offsetTop;
target.addEventListener('mousedown', (e) => {
if (e.buttons === 1) {
target.style.zIndex = 10000
target.style.position = 'relative';
target.isMouseDown = true;
offsetX = target.initialOffsetLeft + e.offsetX;
offsetY = target.initialOffsetTop + e.offsetY;
}
});
document.addEventListener('mouseup', (e) => {
e.preventDefault();
target.style.zIndex = null
target.isMouseDown = false;
target.style.left = 0 + 'px';
target.style.top = 0 + 'px';
});
document.addEventListener('mousemove', (e) => {
e.preventDefault();
if (target.isMouseDown) {
target.style.left = e.pageX - offsetX + 'px';
target.style.top = e.pageY - offsetY + 'px';
}
});
});
})();
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Roboto', sans-serif
}
.draggable {
display: flex;
padding: 10px 12px;
margin-bottom: 11px;
border-radius: 5px;
margin-right: 5px;
background-color: #000000;
cursor: grab;
flex-grow:1;
color: #ffffff;
border: 1px solid #6c757d;
}
.group.card {
margin-top: 30px;
background-color: #000000;
margin-right: 2%;
margin-left: 2%;
border: 1px solid #6c757d;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="group card">
<div class="card-body">
<div class="draggable">
Lorem ipsum dolor sit amet 1
</div>
<div class="draggable">
Lorem ipsum dolor sit amet 2
</div>
<div class="draggable">
Lorem ipsum dolor sit amet 3
</div>
</div>
</div>
As demonstrated in the snippet above, whenever any of the .draggable
elements is dragged, it shifts to the bottom and right. I noticed this stops happening when doing any of: removing bootstrap
or removing all margins from the below:
.group.card {
/*margin-top: 30px;*/
background-color: #000000;
/*margin-right: 2%;*/
/*margin-left: 2%;*/
border: 1px solid #6c757d;
}
By doing so, the drag works as intended. Why is this happening and how to fix?