调整div垂直或水平

时间:2017-04-14 12:00:03

标签: javascript html css

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

3 个答案:

答案 0 :(得分:0)

您可以使用style执行此操作 e.g。

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

答案 1 :(得分:0)

我发现最佳结果如下:

 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

答案 2 :(得分:0)

HTML

<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>

JAVASCRIPT

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