Drag, drop and snap

Size
2,542 Kb
Views
30,360

How do I make an drag, drop and snap?

Drag and drop in a list and snaps in order. What is a drag, drop and snap? How do you make a drag, drop and snap? This script and codes were developed by Darby Rathbone on 03 October 2022, Monday.

Drag, drop and snap Previews

Drag, drop and snap - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>drag, drop and snap</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <ul> <li class='drag'><span>this is 1</span> </li> <li class='drag'><span>this is 2</span> </li> <li class='drag'><span>this is 3</span> </li> <li class='drag'><span>this is 4</span> </li>
</ul><ul> <li class='drag'><span>this is 1</span> </li> <li class='drag'><span>this is 2</span> </li> <li class='drag'><span>this is 3</span> </li> <li class='drag'><span>this is 4</span> </li>
</ul> <script src="js/index.js"></script>
</body>
</html>

Drag, drop and snap - Script Codes CSS Codes

.drag { cursor:move; text-align: center; width:auto; height:auto; border:ridge 3px #cccccc; overflow: hidden; display:flex; padding:10px; margin:5px;
}
ul { outline:inset 3px; overflow:hidden; width:auto;
}

Drag, drop and snap - Script Codes JS Codes

var when = function (event, conditioncallback, truecallback, checkparents, falsecallback) { this.addEventListener(event, function (e) { var p = []; p.push(e.target); if (checkparents) { while (p[p.length - 1].parentElement) { p.push(p[p.length - 1].parentElement) } } if ((!(p.some(function (f) { if (conditioncallback.call(f)) { truecallback.bind(f, e).call(); return true } else { return false } }))) && falsecallback) { falsecallback() } })
};
var dragging, offset;
var dragables = document.getElementsByClassName('drag');
[].slice.call(dragables).reverse().forEach(function (e) { var b = getComputedStyle(e); console.log(b); e.style.top = b.top; e.style.left = b.left
});
var pm = [0, 0];
when('mousedown',
function () { event.preventDefault(); return this.classList.contains('drag')
},
function (e) { var order = [].slice.call(dragables).sort(function (a, b) { return parseInt(getComputedStyle(b)['z-index']) - parseInt(getComputedStyle(a)['z-index']) }); order.unshift(this); order = order.reverse(); order.forEach(function (a, i) { if (!a.style) a.style; a.style['z-index'] = i }); this.style.position = 'relative'; dragging = this; var b = getComputedStyle(this); pm = [e.pageX, e.pageY]; offset = [e.pageX - (parseInt(b.left) || 0), e.pageY - (parseInt(b.top) || 0)]; e.preventDefault()
}, true);
when('mousemove',
function () { return dragging
},
function (e) { var dy = e.pageY - pm[1]; var before = dragging.getBoundingClientRect(); var ev = [e.pageX, e.pageY]; if (dy > 0) { ev[1] = (before.bottom) } if (dy < -0) { ev[1] = (before.top - 1) } var ex = document.elementFromPoint(ev[0], ev[1]); if (ex && dragging !== ex) { if (dragging.parentElement === ex.parentElement) { ex.parentElement.insertBefore(dragging, ex); if (dy > 0) { ex.parentElement.insertBefore(ex, dragging) } var after = dragging.getBoundingClientRect(); offset[1] += after.top - before.top } } if (dy) { pm = [e.pageX, e.pageY]; dragging.style.top = e.pageY - offset[1] + 'px'; dragging.style.left = e.pageX - offset[0] + 'px' } e.preventDefault()
});
when('mouseup',
function () { return dragging
},
function (e) { dragging.style.top = 0 + 'px'; dragging.style.left = 0 + 'px'; e.preventDefault(); dragging.style.position = ''; dragging = false; offset = []
});
Drag, drop and snap - Script Codes
Drag, drop and snap - Script Codes
Home Page Home
Developer Darby Rathbone
Username blackkbot
Uploaded October 03, 2022
Rating 3
Size 2,542 Kb
Views 30,360
Do you need developer help for Drag, drop and snap?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Darby Rathbone (blackkbot) Script Codes
Create amazing marketing copy with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!