css 垂直或水平调整div大小

7eumitmz  于 2023-01-27  发布在  其他
关注(0)|答案(4)|浏览(146)

如何调整div垂直或水平不使用css属性,只是从高度或宽度调整使用纯javascript代码?

uplii1fm

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

rkkpypqq

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

acruukt9

acruukt93#

您可以使用style执行此操作,例如:

getElementById('div_register').style.width='500px';
getElementById('div_register').style.height='500px';
nuypyhwy

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>

相关问题