简体   繁体   English

使用jQuery将div放置在窗口的空白部分的中间

[英]Placing div in the middle of empty portion of window using jquery

In my template I have fixed div at the left of the main wrapper div , In the remaining portion I want to place my another div box perfectly in the middle even on window re-size. 在我的模板中,我在主包装div的左侧固定了div,在其余部分中,我想将另一个div框完美地放置在中间,即使在调整窗口大小时也是如此。

I got the current window-size with jquery: 我得到了当前窗口大小与jQuery:

<script type=text/javascript>
$(window).resize(function() {
       var my_window = $(window).width();
});
</script>

Html: HTML:

<div id="wrapper">
<div id="rightbox"></div>
<div id="leftbox"> </div>
</div>

Css: CSS:

#wrapper{
position:absolute;
width:100%;
height:100%;
background:green;}

#leftbox{
position:fixed;
width:100px;
height:100%;
top:0;left:0;
background:red;}


#rightbox{
position:fixed;
width:400px;
height:100px;
bottom:100px;
left:50px;
background:blue;}

Demo : http://jsfiddle.net/NpeRZ/ 演示: http : //jsfiddle.net/NpeRZ/

Now I want to place rightbox div in the middle of the green screen for any screen-resolution, so I want flexible width for this rightbox div . 现在,我想将rightbox div放置在绿色屏幕的中间,以获得任何屏幕分辨率,因此我想要此rightbox div灵活宽度。 How to get this using jquery to set rightbox always in the middle. 如何使用jquery将此设置始终设置在rightbox中。 As well according to jquery my_window value I want to set different margin-left and margin-right for rightbox div. 同样根据jquery my_window值,我想为rightbox div设置不同的margin-left和margin-right。

for my-window 1024px :margin-left = 100px,
for my-window 768px  :margin-left = 80px.
for my-window 640px  :margin-left = 60px.
for my-window 480px  :margin-left = 40px.
for my-window 240px  :margin-left = 20px. 

try this: 尝试这个:

#rightbox{
position:fixed;
width:400px;
height:100px;
left: 100px;
right: 0px;
top: 0px;
margin: auto;
bottom:0px;
background:blue;
}

The left would be the size of of the left frame or div. 左侧将是左侧框架或div的大小。

You don't have to use Jquery, just change the css of the right box to: 您不必使用Jquery,只需将右侧框的CSS更改为:

#rightbox{
position:fixed;
width:400px;
height:100px;
left:50%;
margin-left:-150px;    
top:50%;
margin-top:-50px;
background:blue;}

Just tried it, works perfectly. 刚刚尝试过,效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM