Html5通过拖动调整对象大小

时间:2015-05-20 19:10:43

标签: javascript html5

我需要在我的小应用程序中调整音符大小,但我不知道如何。它们必须通过拖动它们的右下角来调整大小,它必须在纯java脚本中完成。

分配" +"添加新笔记和空div就像所有笔记的计数器一样。

代码:



document.addEventListener("onload", Load());
	var index = 0;
	var cnt = 0;
	var x = 0;
	var y = 0;
	var xx = 0;
	var yy = 0;
	var clicked = false;
	var dragged = false;
	var counter = 0;
	var numberOfPapers = 0;
	var state = 0;
	
	
	function Load(){
		var adder = document.querySelector("#add");
		adder.setAttribute("onclick", "addClick()");
	}
	
	function addClick(){
		cnt++;
		numberOfPapers++;
		document.querySelector("#counter").innerHTML = "Przebieg = " + cnt + "<br>" + "Liczba kartek = " + numberOfPapers;
		var paper = document.createElement("div");
		var paperX = document.createElement("div");
		var paperR = document.createElement("div");
		var paperS = document.createElement("div");
		//papierek xD
		paper.setAttribute("class", "paper");
		paper.setAttribute("onmousedown", "movePaper(this,event)");
		paper.setAttribute("onmouseup", "stop(this)");
		paper.setAttribute("onmouseleave", "stop(this)");
		paper.setAttribute("id", "id_" + cnt);
		paper.style.top = "100px";
		paper.style.left = "100px";
		paper.style.zIndex = cnt;
		//niszczyciel papierków
		paperX.setAttribute("class", "deleter");
		paperX.setAttribute("onclick", "deletePaper(this)");
		//zmieniacz rozmiarów
		paperR.setAttribute("class", "resizer");
		paperR.ondragstart = function(e){ 
			e.preventDefault();
			};
		paperR.setAttribute("onmousedown", "resize(this,event)");
		//edytor tekstu tini emce
		paperS.setAttribute("class", "txtEditor");
		paperS.setAttribute("onclick", "editTxt()");
		
		paper.appendChild(paperX);
		paper.appendChild(paperR);
		paper.appendChild(paperS);
		
		document.body.appendChild(paper);
	}
	
	function stop(e){
		e.setAttribute("onmousemove", null);
		state = 1;
	}
	
	function resize(e,event){
		state = 2;
		
	}
	
	function deletePaper(e){
			e.parentElement.id = "del";
			var del = document.querySelector("#del");
			del.parentNode.removeChild(del);
			numberOfPapers--;
			document.querySelector("#counter").innerHTML = "Przebieg = " + cnt + "<br>" + "Liczba kartek = " + numberOfPapers;
		}
	
	function movePaper(e, event){
		index++;
		e.style.zIndex = index;
		x = event.clientX;
		y  = event.clientY;
		xx = e.style.left;
		yy = e.style.top;
		xx = xx.slice(0,xx.search("px"));
		yy = yy.slice(0,yy.search("px"));
		x = x - xx;
		y = y - yy;
		
		e.setAttribute("onmousemove","moreMove(this,event)");
	}
	
	function moreMove(e,event){
		e.style.top = event.clientY - y + "px";
		e.style.left = event.clientX - x + "px";
	}
	
	
&#13;
body{
		margin: 0;
		padding: 0;
		}
		
	#add{
		position: absolute;
		width: 45px;
		height: 35px;
		top: 25px;
		right: 25px;
		background-color: #F5574E;
		text-align:center;
		padding-top:10px;
		border: solid black 1px;
		}
	
	#counter{
		position: absolute;
		width: 200px;
		height: 45px;
		top: 25px;
		right: 80px;
		background-color: #F5574E;
		text-align:center;
		border: solid black 1px;
		}
	
	.paper{
		position: absolute;
		width: 100px;
		height: 100px;
		top: 25px;
		left: 25px;
		background-color: #E3D67F;
		border: solid black 1px;
		-webkit-touch-callout: none;
            -webkit-user-select: none;
            -khtml-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
		}
		
	.deleter{
		position: absolute;
		width: 10px;
		height: 10px;
		top: 0px;
		right: 0px;
		background-color: red;
		}
		
	.resizer{
		position: absolute;
		width: 10px;
		height: 10px;
		bottom: 0px;
		right: 0px;
		background-color: green;
		}
	
	.txtEditor{
		position: absolute;
		width: 10px;
		height: 10px;
		top: 10px;
		right: 0px;
		background-color: yellow;
		}
&#13;
<body>
<div id="add">+
</div>
<div id="counter">
</div>
    
</body>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您可以简单地复制移动功能,而不是定位顶部和左侧的目标父节点的宽度和高度。像这样:

function resize(e, event) {
    event.stopPropagation();//this to prevent move behavior to be triggered when clicking resize handle
    state = 2;
    index++;
    e.style.zIndex = index;
    x = event.clientX;
    y = event.clientY;
    xx = e.parentNode.style.width;
    yy = e.parentNode.style.height;
    xx = xx.slice(0, xx.search("px"));
    yy = yy.slice(0, yy.search("px"));
    x = x - xx;
    y = y - yy;

    e.setAttribute("onmousemove", "resizeMove(this,event)");
}

function resizeMove(e, event) {
    console.log('resixe')
    e.parentNode.style.height = event.clientY - y + "px";
    e.parentNode.style.width = event.clientX - x + "px";
}

您必须声明parentNode的宽度和高度才能生效,您可以将其添加到纸质部分。

paper.style.width = "100px";
paper.style.height = "100px";