Javascript 当窗口大小更改时,保持根div居中到页面
请参考下面的代码Javascript 当窗口大小更改时,保持根div居中到页面,javascript,jquery,html,css,Javascript,Jquery,Html,Css,请参考下面的代码 <div id="root"> <div id="child1">xxxx</div> <div id="child2">yyyy</div> </div> 它将使“根”div居中于页面中心。但某些分辨率未对齐(即居中) 因此,我检查javascript/jquery代码,而不是使用媒体查询(因为我使用iE8浏览器-不支持媒体) 如果(window.screen.availWidth>=1200&&wi
<div id="root">
<div id="child1">xxxx</div>
<div id="child2">yyyy</div>
</div>
它将使“根”div居中于页面中心。但某些分辨率未对齐(即居中)
因此,我检查javascript/jquery代码,而不是使用媒体查询(因为我使用iE8浏览器-不支持媒体)
如果(window.screen.availWidth>=1200&&window.screen.availWidth尝试将其放入.resize()
方法:
$(window).on('resize', function(){
if ($(this).width() >= 1200 && $(this).width() <= 1390 ) {
$("#root").css("width","92%");
}else{
$("#root").css("width","86%");
}
}).resize();
$(窗口).on('resize',function()){
如果($(this).width()>=1200&&$(this).width()否,则需要使用任何脚本来保持居中,CSS也可以这样做:
这是当时我需要设置的宽度,我不知道?我知道窗口大小调整方法我不知道宽度的百分比参见“宽度的百分比”是以像素为单位计算的,我的意思是,如果我们使用css meadia查询,我们必须在px
中选择设备宽度,因此最好使用js中的条件,如上所述。92%和86%只是在通过开发工具对齐内容后给出一些手动值,但在调整窗口大小时,我如何知道是什么我需要设置为中心的确切宽度?宽度:86%//
<script type"text/javascript">
if (window.screen.availWidth >= 1200 && window.screen.availWidth <= 1390 ) {
$("#root").css("width","92%")
}
else
$("#root").css("width","86%")
</script>
$(window).on('resize', function(){
if ($(this).width() >= 1200 && $(this).width() <= 1390 ) {
$("#root").css("width","92%");
}else{
$("#root").css("width","86%");
}
}).resize();
#root
{
width:86% // <-------here you have a missing ';'
margin:0 auto;
}