javascript - 获取列的高度和宽度并在调整大小时更新

标签 javascript jquery css

我有这个 HTML:

<div class="columna1"> CONTENT1 </div>
<div class="columna2"> CONTENT2 </div>

还有这个 JavaScript:

column1_height = $('#columna1').height();
column1_width = $('#columna1').width();
column2_height = $('#columna2').height();
column2_width = $('#columna2').width();

但是当我调整屏幕大小时,值不会更新。

谢谢

最佳答案

试试这个:

$(document).on('ready', initFunction);
$(window).on('resize', getSizes);

var column1_height = 0;
var column1_width = 0;
var column2_height = 0;
var column2_width = 0;

function initFunction(){
    getSizes();
}

function getSizes(){
    column1_height = $('#columna1').height();
    column1_width = $('#columna1').width();
    column2_height = $('#columna2').height();
    column2_width = $('#columna2').width();

    console.log("Column 1: (Height: " +column1_height+ ") - (Width: " +column1_width+ ")");
    console.log("Column 2: (Height: " +column2_height+ ") - (Width: " +column2_width+ ")");
}

这应该有效

关于javascript - 获取列的高度和宽度并在调整大小时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36068810/

相关文章:

javascript - 具有更高级别值的复杂嵌套 sequelize 循环查询

javascript - getElementByID + .appendhild() [类型错误 : Failed to execute 'appendChild' on 'Node' : parameter 1 is not of type 'Node' . ]

javascript - ie8 中生成的 html 未添加不透明度

javascript - 为什么 Mozilla Firefox 不处理正确的 jQuery?

html - CSS : How to do the reverse of line wrapper

html - 位于两个独立 div 的顶部和底部

php - 如何在同一页面中显示html输出?

javascript - NgFor 无法工作 Angular 2

javascript - 在字符串中传递的 HTML 标签无法正常渲染

html - 父溢出时如何设置子div宽度100%?