简体   繁体   English

使用Javascript中的比例调整div的大小(不使用Jquery)

[英]Resize a div with a ratio in Javascript (without Jquery)

I'm coding an editing tool in Javascript (I can't use Jquery and HTML Canvas for this project). 我正在用Javascript编写一个编辑工具(我不能在这个项目中使用Jquery和HTML Canvas)。 So the goal is to use the mouse on the edge of a div (ex : a square) and resize the div proportionally (use a ratio). 因此,目标是在div的边缘使用鼠标(例如:正方形)并按比例调整div的大小(使用比率)。 My code right now can resize the square but not proportionnaly. 我现在的代码可以调整大小但不是按比例调整大小。 I'd like to make this " https://jqueryui.com/resizable/#aspect-ratio " in pure javascript. 我想在纯JavaScript中制作这个“ https://jqueryui.com/resizable/#aspect-ratio ”。

My Code : 我的代码:

 var resizeHandle = document.getElementById('handle_bottom_right'); var box = document.getElementById('box'); resizeHandle.addEventListener('mousedown', initialiseResize, false); var scale = 1.1 function initialiseResize(e) { window.addEventListener('mousemove', startResizing, false); window.addEventListener('mouseup', stopResizing, false); } function startResizing(e) { box.style.width = (e.clientX - box.offsetLeft) + 'px'; box.style.height = (e.clientY - box.offsetTop) + 'px'; } function stopResizing(e) { window.removeEventListener('mousemove', startResizing, false); window.removeEventListener('mouseup', stopResizing, false); } function doubleClicked(element){ setTimeout(function(){ if(document.activeElement !== element){ element.contentEditable = false; } }, 300); } 
 #box { position: relative; width: 150px; height: 150px; background-color: #2196F3; color: white; display:flex; justify-content:center; align-items:center; border-radius: 10px; } #handle_bottom_right { background-color: #727272; width: 10px; height: 10px; cursor: se-resize; position:absolute; right: 0; bottom: 0; } 
 <div id="box" onclick="showCoords(event);"> <p onclick="this.contentEditable=true; doubleClicked(this);" onblur="this.contentEditable=false;" contenteditable="false">double click me to change me</p> <div id="handle_bottom_right"></div> </div> <p id="demo"></p> <p id="demo1"></p> 

Is there someone who has any idees how to solve this? 是否有人有任何想法如何解决这个问题?

 var resizeHandle = document.getElementById('handle_bottom_right'); var box = document.getElementById('box'); resizeHandle.addEventListener('mousedown', initialiseResize, false); var scale = 1.1 function initialiseResize(e) { window.addEventListener('mousemove', startResizing, false); window.addEventListener('mouseup', stopResizing, false); } function startResizing(e) { var w = (e.clientX - box.offsetLeft); box.style.width = w + 'px'; //box.style.height = (e.clientY - box.offsetTop) + 'px'; box.style.height = Math.round(scale*w)+'px'; } function stopResizing(e) { window.removeEventListener('mousemove', startResizing, false); window.removeEventListener('mouseup', stopResizing, false); } function doubleClicked(element){ setTimeout(function(){ if(document.activeElement !== element){ element.contentEditable = false; } }, 300); } 
 #box { position: relative; width: 150px; height: 150px; background-color: #2196F3; color: white; display:flex; justify-content:center; align-items:center; border-radius: 10px; } #handle_bottom_right { background-color: #727272; width: 10px; height: 10px; cursor: se-resize; position:absolute; right: 0; bottom: 0; } 
 <div id="box" onclick="showCoords(event);"> <p onclick="this.contentEditable=true; doubleClicked(this);" onblur="this.contentEditable=false;" contenteditable="false">double click me to change me</p> <div id="handle_bottom_right"></div> </div> <p id="demo"></p> <p id="demo1"></p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM