我正在尝试做这个 fiddle 的逆操作,制作一个宽度基于 100% 高度的正方形。
<html style="height:100%">
<body style="height:100%">
<div id="square" style="background-color:black"></div>
</body>
</html>
$(window).ready(updateWidth);
$(window).resize(updateWidth);
function updateWidth()
{
var square = $('#square');
var size = square.width();
square.css('height',size);
}
非常感谢您的帮助。
七月。
最佳答案
在 CSS 中也可以设置 div 的高度
<style>
html,body,#square { height:100%; }
</style>
然后你的js函数反过来
function updateWidth()
{
var square = $('#square');
var size = square.height();
square.css('width',size);
}
由 wared 提供的演示 - jsfiddle.net/wared/spSLP - 不错,wared
关于javascript - 宽度基于百分比高度的方形 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20936061/