修改jQuery函数以调整DIV大小



i当前使用jQuery函数创建正方形div。平方大小基于浏览器变量的高度。它运行良好。

<script>
$(window).ready(updateWidth);
$(window).resize(updateWidth);
function updateWidth()
{
var square = $('.square');
var size = square.height();
square.css('width',size);
}
</script>

.square {
height:100%;
float:left;
position:relative;
}

我想做的是:对于使用此功能(.HALF)的某些DIV,将宽度除以两个。

对于" .square"div:保持宽度,保持高度;

对于" .square .half"div:保持高度,新宽度=宽度/2;

我不知道如何修改我的功能,如果有人可以帮助我。

谢谢,

塞巴斯蒂安

尝试以下:

function updateWidth()
{
    var square = $('.square');
    var squareHalf = $('.half');
    var size = square.height();
    square.css('width',size);
    squareHalf.css({'width':''+size/2+'px','height':''+size/2+'px'});
}

demo

嗨,尝试一下。您需要在Documnet上设置调整大小事件。

 $(document).ready(function(){
   $(window).resize(function(){
     put your code here
});
});
});

谢谢,

最新更新