Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 宽度等于动态高度平方_Javascript_Css_Dynamic_Height_Width - Fatal编程技术网

Javascript 宽度等于动态高度平方

Javascript 宽度等于动态高度平方,javascript,css,dynamic,height,width,Javascript,Css,Dynamic,Height,Width,当高度为父div的%时,css或js是否可以设置元素的宽度以匹配其高度。如果调整浏览器窗口的大小,子div也需要更改大小 示例 body, html {height: 100%} .parent {height: 100%; width: 960px;} .child { height: 50%; width: same as height; /* but not 50% of 960px, so that the child div is square*/ } 我正在努力实

当高度为父div的%时,css或js是否可以设置元素的宽度以匹配其高度。如果调整浏览器窗口的大小,子div也需要更改大小

示例

body, html {height: 100%}
.parent {height: 100%; width: 960px;}
.child {
    height: 50%; 
    width: same as height; /* but not 50% of 960px, so that the child div is square*/
}
我正在努力实现这样的目标,但不是高度而是宽度。

提前感谢:-)


您可以使用jQuery来匹配元素的高度和宽度。

如果使用jQuery,可以在
resize()
函数中设置
width=height
,反之亦然

$(窗口)。调整大小(函数(){
$('main')。高度($('main')。宽度();
});

#主{
位置:绝对位置;
底部:0;
排名:0;
边框:#000薄实心;
背景:#DDEEFF;
宽度:100%;
}
使用jquery

function resizeChild() {
var child = $(".child");
var parentSize = child.parent().height();
child.height(parentSize / 2);
child.width(parentSize / 2);
}
$(window).ready(resizeChild);
$(window).resize(resizeChild);

Javascript,我的朋友。只需拖动
结果
帧,然后观察高度与宽度的匹配情况,即
100%
帧的宽度。
function resizeChild() {
var child = $(".child");
var parentSize = child.parent().height();
child.height(parentSize / 2);
child.width(parentSize / 2);
}
$(window).ready(resizeChild);
$(window).resize(resizeChild);