javascript - Creating new div onmousedown -
hi create new div onmousedown , resize on mouse move. can't seem mistake i've made.
var x1; var y1; var pressed = false; document.getelementbyid("primary").onmousedown = function() { pressed = true; var ok = true; if (ok === true) { div = document.createelement('div'); div.style.backgroundcolor = "black"; div.style.position = "absolute"; x1 = math.round(event.clientx); y1 = math.round(event.clienty); div.style.left = x1 + "px"; div.style.top = y1 + "px"; div.setattribute("id", "uniqueidentifier"); document.getelementsbytagname('body')[0].appendchild(div); } }; document.getelementbyid("primary").onmousemove = function() { if (pressed) { var div = get.getelementbyid("uniqueidentifier"); var x2 = math.round(event.clientx) + x1; var y2 = math.round(event.clienty) + y1; div.style.width = x2 + "px"; div.style.height = y2 + "px"; } } document.getelementbyid("primary").onmouseup = function() { pressed = false; }
#primary { height: 200px; width: 200px; background-color: yellow; }
<div id="primary"></div>
you had typo. get.getelementbyid
should document.getelementbyid
. when fix that, code works.
var x1; var y1; var pressed = false; document.getelementbyid("primary").onmousedown = function() { pressed = true; var ok = true; if (ok === true) { div = document.createelement('div'); div.style.backgroundcolor = "black"; div.style.position = "absolute"; x1 = math.round(event.clientx); y1 = math.round(event.clienty); div.style.left = x1 + "px"; div.style.top = y1 + "px"; div.setattribute("id", "uniqueidentifier"); document.getelementsbytagname('body')[0].appendchild(div); } }; document.getelementbyid("primary").onmousemove = function() { if (pressed) { var div = document.getelementbyid("uniqueidentifier"); var x2 = math.round(event.clientx) + x1; var y2 = math.round(event.clienty) + y1; div.style.width = x2 + "px"; div.style.height = y2 + "px"; } } document.getelementbyid("primary").onmouseup = function() { pressed = false; }
#primary { height: 200px; width: 200px; background-color: yellow; }
<div id="primary"></div>
Comments
Post a Comment