All pastes #993521 Raw Edit

Anonymous

public text v1 · immutable
#993521 ·published 2008-04-22 18:06 UTC
rendered paste body
var dragObject, offsetX, offsetY, isDragging=false;

document.onmousemove = mM;
document.onmouseup = mU;
e = window.event;

function init (init_top, init_left, init_where) {
	var ob = document.getElementById(init_where);
	ob.ondrag = function() {
		return false;
	}
	
	ob.onselectstart = function() {
		return false;
	}
	ob.style.position = "absolute";

	ob.style.left = init_left;
	ob.style.top = init_top;
}


function mD (ob,e) {
	dragObject = ob;

	var dragX = parseInt(dragObject.style.left);
	var dragY = parseInt(dragObject.style.top);

	var mouseX = e.clientX;
	var mouseY = e.clientY;

	offsetX = mouseX - dragX;
	offsetY = mouseY - dragY;

	isDragging = true;

	return false;
}

function mM (e) {
	e.onselect = function () {
		return false;
	}
	if (isDragging) {
	
		var newX = e.clientX - offsetX;
		var newY = e.clientY - offsetY;

		dragObject.style.left = newX + "px";
		dragObject.style.top = newY + "px";

		if (parseInt(dragObject.style.left) < 0)
			dragObject.style.left = "0px";
		if (parseInt(dragObject.style.top) < 0)
			dragObject.style.top = "0px";

		if (parseInt(dragObject.style.left) > (document.documentElement.clientWidth-parseInt(dragObject.style.width)))
			dragObject.style.left = (document.documentElement.clientWidth-parseInt(dragObject.style.width)) + "px";
		if (parseInt(dragObject.style.top) > (document.documentElement.clientHeight-parseInt(dragObject.style.height)))
			dragObject.style.top = (document.documentElement.clientHeight-parseInt(dragObject.style.height)) + "px";

		return false;
	}

	return;
}

function mU () {
	if (isDragging) {
		isDragging = false;

		return false;
	}

	return;
}