gpt4 book ai didi

javascript - 窗口调整大小问题

转载 作者:行者123 更新时间:2023-11-28 02:46:41 25 4
gpt4 key购买 nike

我想留出一个网络浏览器调整边距。我使用windowResize,但是即使自定义单击网络浏览器的恢复按钮,如何调整左侧边距?我上传了 2 张屏幕截图进行解释。 alt text第一张图片,我打开网络浏览器而不是最大化,因此页面调整了窗口大小并计算了左边距,然后我单击窗口最大化,然后得到 alt text第二张图片,但内容不在页面中央。如何正确调整窗口大小?谢谢。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
var width = document.body.clientWidth;
windowResize(width);
$(window).resize(function() {
windowResize(width);
});
});
function windowResize(width) {
if(width>1024){
$('#content').css({
'margin-left':(width-1024)/2+32 + 'px'
});
}else{
$('#content').css({
'margin-left': 32 + 'px'
});
}
}
</script>
</head>
<body>
<style>
body, #box{min-width:1024px;width:100%;}
#content{float:left;width:960px;height:300px;background-color:#F00;}
</style>
<div id="box">
<div id="content">
some words
</div>
</div>
</body>
</html>

最佳答案

我注意到的一件事是,此代码只会使用初始宽度调用 windowResize:

jQuery(document).ready(function() {    
// this value will always be passed to windowResize
var width = document.body.clientWidth;
windowResize(width);
$(window).resize(function() {
windowResize(width);
});
});

我更改了它,以便在 windowResize 内部计算宽度,并且一切正常。

jQuery(document).ready(function() {    
windowResize();
$(window).resize(windowResize);
});

function windowResize() {
var width = document.body.clientWidth;
if(width>1024){
$('#content').css({
'margin-left':(width-1024)/2+32 + 'px'
});
}else{
$('#content').css({
'margin-left': 32 + 'px'
});
}
}

关于javascript - 窗口调整大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4768651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com