Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.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 宽度基于高度百分比的方形div_Javascript_Jquery_Html - Fatal编程技术网

Javascript 宽度基于高度百分比的方形div

Javascript 宽度基于高度百分比的方形div,javascript,jquery,html,Javascript,Jquery,Html,我试着做这个小提琴的反面,做一个宽度基于100%高度的正方形 $(窗口).ready(更新宽度); $(窗口)。调整大小(更新宽度); 函数updateWidth() { 变量平方=$('平方'); var size=square.width(); square.css(‘高度’、大小); } 非常感谢你的帮助 塞布 注意-这首先需要正方形div有一个高度-高度和宽度的行为不一样-只是一个平头 在CSS中设置div的高度 <style> html,body,#square {

我试着做这个小提琴的反面,做一个宽度基于100%高度的正方形


$(窗口).ready(更新宽度);
$(窗口)。调整大小(更新宽度);
函数updateWidth()
{
变量平方=$('平方');
var size=square.width();
square.css(‘高度’、大小);
}
非常感谢你的帮助

塞布


注意-这首先需要正方形div有一个高度-高度和宽度的行为不一样-只是一个平头

在CSS中设置div的高度

<style>

html,body,#square { height:100%; }

</style>

演示由wared提供-jsfiddle.net/wared/spSLP--nice one,wared

在变量中使用简单的数学公式,您可以设置一个自动调整大小的平方div。 更改*后面的100,为div指定一个%的宽度。 请参见工作以获得响应宽度

$(document).ready(function() { 

  var height =  ( $(window).height() / 100) * 100 ;
  $('#square').width(height);
  $('#square').height(height); 

    });

$(window).resize(function(){

 var height =  ( $(window).height() / 100) * 100 ;
 $('#square').width(height);
 $('#square').height(height); 

    });

如果有人能帮我修改这个代码,使之成为一个适合100%高度的正方形。(px中的宽度必须等于%x中的高度)
<style>

html,body,#square { height:100%; }

</style>
function updateWidth()
{
var square = $('#square');
var size = square.height();

square.css('width',size);
}
$(document).ready(function() { 

  var height =  ( $(window).height() / 100) * 100 ;
  $('#square').width(height);
  $('#square').height(height); 

    });

$(window).resize(function(){

 var height =  ( $(window).height() / 100) * 100 ;
 $('#square').width(height);
 $('#square').height(height); 

    });