如何调整div垂直或水平不使用css属性,只是从高度或宽度调整使用纯javascript代码?
uplii1fm1#
超文本标记语言
<div id="container"> <div id="top-panel"> This is the top side's content! </div> <div id="bottom-panel"> <div id="drag"></div> bottom content!</div>
java 脚本
var isResizing = false, lastDownX = 0; $(function () { var container = $('#container'), top = $('#top-panel'), bottom = $('#bottom-panel'), handle = $('#drag'); document.addEventListener("mousedown", function(e){ isResizing = true; lastDownX = e.clientY; }); document.addEventListener("mousemove", function(e){ // we don't want to do anything if we aren't resizing. if (!isResizing) return; console.log("e.clientY ", e.clientY, container.offset().top) var offsetRight = container.height() - (e.clientY - container.offset().top); top.css('bottom', offsetRight); bottom.css('height', offsetRight); }); document.addEventListener("mouseup", function(e){ // stop resizing isResizing = false; });
}); resize div veritcally
rkkpypqq2#
我发现最好的结果如下:
var div1 = document.getElementById("DivOne"); var dRect = div1.getBoundingClientRect(); var scrollBarWidth = 6; var w = window.innerWidth - 2*dRect.left - scrollBarWidth; var h = window.innerHeight - 2*dRect.top - scrollBarWidth; div1.style.width = w.toString() + "px"; div1.style.height = h.toString() + "px";
工作示例:Single DIV sized to the document window
acruukt93#
您可以使用style执行此操作,例如:
style
getElementById('div_register').style.width='500px'; getElementById('div_register').style.height='500px';
nuypyhwy4#
function handleContainerHeight(e) { const container = document.querySelector("#elmentToResize"); container.style.height = `${e.clientY}px`; } const handleDragMouseDown = (e) => { document.addEventListener("mousemove", handleContainerHeight); document.addEventListener("mouseup", handleDragMouseUp, { once: true }); }; const handleDragMouseUp = (e) => { document.removeEventListener("mousemove", handleContainerHeight); };
<div className="resize-container"> <div className="resize-wrapper" onMouseDown={handleDragMouseDown}> <img src="image.png" alt="Drag Icon" /> </div> </div>
4条答案
按热度按时间uplii1fm1#
超文本标记语言
java 脚本
}); resize div veritcally
rkkpypqq2#
我发现最好的结果如下:
工作示例:Single DIV sized to the document window
acruukt93#
您可以使用
style
执行此操作,例如:nuypyhwy4#