var newX = posLeft + mousePosX - obj.mousePosX; var newY = posTop + mousePosY - obj.mousePosY; newX = newX < 0 ? 0 : newX; newY = newY < 0 ? 0 : newY; obj.style.left = newX + 'px'; obj.style.top = newY + 'px';
function drag_object( evt, obj ) { evt = evt || window.event; obj.clicked = true; obj.mousePosX = evt.clientX; obj.mousePosY = evt.clientY; if( evt.preventDefault ) evt.preventDefault(); else evt.returnValue = false; document.onmouseup = function(){ obj.clicked = false } document.onmousemove = function( evt ) { evt = evt || window.event; if( obj.clicked ) { posLeft = !obj.style.left ? obj.offsetLeft : parseInt( obj.style.left ); posTop = !obj.style.top ? obj.offsetTop : parseInt( obj.style.top ); mousePosX = evt.clientX; mousePosY = evt.clientY; var newX = posLeft + mousePosX - obj.mousePosX; var newY = posTop + mousePosY - obj.mousePosY; var newXright = newX + obj.offsetWidth; var newYbottom = newY + obj.offsetHeight; var scrollHeight = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight); var scrollWidth = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth); newX = newX < 0 ? 0 : newX; newY = newY < 0 ? 0 : newY; newX = newXright > scrollWidth ? scrollWidth - obj.offsetWidth : newX; newY = newYbottom > scrollHeight ? scrollHeight - obj.offsetHeight : newY; obj.style.left = newX + 'px'; obj.style.top = newY + 'px'; obj.mousePosX = mousePosX; obj.mousePosY = mousePosY; if(mousePosX < 0 || mousePosY < 0 || mousePosX > scrollWidth || mousePosY > scrollHeight) obj.clicked = false; } } }
function resizeObject( evt, obj ) { evt = evt || window.event; obj.resize = true; obj.mousePosX = evt.clientX; obj.mousePosY = evt.clientY; if( evt.preventDefault ) evt.preventDefault(); else evt.returnValue = false; document.onmouseup = function(){ obj.resize = false } document.onmousemove = function( evt ) { evt = evt || window.event; if( obj.resize ) { objWidth = !obj.style.width ? obj.offsetWidth : parseInt( obj.style.width ); objHeight = !obj.style.height ? obj.offsetHeight : parseInt( obj.style.height ); mousePosX = evt.clientX; mousePosY = evt.clientY; obj.style.width = objWidth + mousePosX - obj.mousePosX + 'px'; obj.style.height = objHeight + mousePosY - obj.mousePosY + 'px'; obj.mousePosX = mousePosX; obj.mousePosY = mousePosY; } } } function createModal() { var elDiv = document.createElement('div'); elDiv.className = 'drag-window'; document.body.appendChild(elDiv); elSpn = document.createElement('span'); elDiv.appendChild(elSpn); elSpn.className = 'title'; elSpn.innerHTML = 'Активная область <span title="закрыть" class="close-button" onmousedown="deleteModal(this.parentNode.parentNode)">×</span>'; elSpn.onmousedown = function(event){dragObject(event || window.event, this.parentNode);}; elContent = document.createElement('div'); elDiv.appendChild(elContent); elContent.className = 'content'; elContent.innerHTML = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna <a href="#">aliquam erat</a> volutpat.'; elResize = document.createElement('div'); elDiv.appendChild(elResize); elResize.className = 'resizeButton'; elResize.innerHTML = '<img src="http://imgs.su/tmp/2012-10-07/1349554018-492.jpg" alt="" onmousedown="resizeObject(event, this.parentNode.parentNode)" />'; } function deleteModal(elem) { elem.parentNode.removeChild(elem); }