JSFiddle - React, Tailwind, and code Playground

by moob

HTML

<h2>Test resizeable and moveable</h2>
<h4>Block1 without header</h4>
<div class="area">
	<div class="block freemover">
		<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
	</div>
</div>
<h4>Block2 with header</h4>
<div class="area">
	<div class="block freemover">
		<h4 class="anchor">Move me</h4>
		<p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
	</div>
</div>

CSS

.area {
		  position: relative;
		  height: 160px;
		}

		.block {
		  position: absolute;
		  top: 15px;
		  left: 15px;
		  z-index: 10;
		  resize: both;
		  overflow: auto;
		  width: 240px;
		  height: 120px;
		  background-color: #ddd;
		}

JavaScript

class initFreeMovers {
	constructor(classname='freemover', options={}) {
		this.classname = classname;
		this.options = options;
		this.M = {};
		this.movers = [];
		document.addEventListener('DOMContentLoaded', () => {this.initialize();});
	}
	initialize() {
		console.log('initFreeMovers - initialize');
		this.M = Array.from(document.getElementsByClassName(this.classname));
		this.M.forEach((elmnt) => {
			this.movers.push(new freeMover(elmnt, this.options));
		});
	}
}
class freeMover {
	constructor(elmnt, options) {
		console.log('freeMover - elmnt', elmnt);
		this.elmnt = elmnt;
		this.pos = [0, 0, 0, 0];
		const anchor = elmnt.getElementsByClassName(options.anchor);
		console.log('freeMover - anchor', anchor);
		
		if (anchor[0]) {
			anchor[0].onmousedown = this.dragMouseDown.bind(this);
		} else {
			elmnt.onmousedown = this.dragMouseDown.bind(this);
		}
	}
	dragMouseDown(e) {
		e = e || window.event;
		//console.log('dragMouseDown - event', e);
		this.pos[2] = e.clientX;
		this.pos[3] = e.clientY;
    console.log(e.target);
		this.elmnt.classList.add('dragging');
		document.onmouseup = this.closeDragElement.bind(this);
		document.onmousemove = this.elementDrag.bind(this);
	}
	elementDrag(e) {
		e = e || window.event;
		this.pos[0] = this.pos[2] - e.clientX;
		this.pos[1] = this.pos[3] - e.clientY;
		this.pos[2] = e.clientX;
		this.pos[3] = e.clientY;
		this.elmnt.style.top = (this.elmnt.offsetTop - this.pos[1]) + "px";
		this.elmnt.style.left = (this.elmnt.offsetLeft - this.pos[0]) + "px";
	}
	closeDragElement() {
		this.elmnt.classList.remove('dragging');
		document.onmouseup = null;
		document.onmousemove = null;
	}
}

const mover = new initFreeMovers('freemover', {anchor: 'anchor'});