简体   繁体   English

如何使用 jQuery 在页面中间居中 div 并在重新调整 window 大小时使其居中?

[英]How do I center a div in the middle of the page using jQuery and keep it centered when window is re-sized?

Does anyone know of any jQuery Script that will keep a div centered in the middle and when the window is re-sized, it will also keep it centered?有谁知道任何 jQuery 脚本可以保持 div 居中,并且当 window 重新调整大小时,它也会保持居中? I am building a lightbox plugin and I need it to stay in the middle.我正在构建一个灯箱插件,我需要它保持在中间。 I tried using CSS but it doesn't work.我尝试使用 CSS 但它不起作用。

Also I need it to stay in the middle when another box is opened that has a bigger width and height.另外,当打开另一个具有更大宽度和高度的盒子时,我需要它保持在中间。 Here's the page with examples on it:这是带有示例的页面:

http://wowbox.yolasite.com/ http://wowbox.yolasite.com/

Open the image first, and then open the div, and you'll see what I mean.先打开图片,再打开div,你就明白我的意思了。 The div is not centered. div 不居中。 But then when you close and re-open the div it is centered because of the.center() that happens when you click on a WowBox link.但是,当您关闭并重新打开 div 时,它会居中,因为当您单击 WowBox 链接时会发生 .center() 。 I just need it to always be centered, and never mess up like that.我只需要它始终处于中心位置,并且永远不会那样混乱。

Here is the code I'm currently using to center it:这是我目前用来居中的代码:

  jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
  }

I want to make it where when the window re-sizes, it will stay centered.我想使它在 window 重新调整大小时保持居中。 I also want to make it where when the width of the box gets wider and the height gets taller, it will re-center.我还想让它在盒子的宽度变宽且高度变高时重新居中。 Right now it doesn't work right.现在它不能正常工作。

Could anyone help with this?有人可以帮忙吗?

Thanks in advance,提前致谢,

Nathan弥敦道

Try that along with the example code you already have.与您已有的示例代码一起尝试。 For clarification:为了澄清:

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}

$(document).ready(function(){
   $('.element').center();
   window.onresize = function(event) {
        $('.element').center();
    }
});

----------------- This works--------------------- -----------------有效---------------------

CSS CSS

       .box{
    width:200px;
    top:-5px; 

    position:relative;
    background:#444;
}
.inner {
    height:0px;
    width:0px;
    background:#444;
    top:50%;
    left:50%;
    right:50%;
    bottom:50%;
    position:relative;
}

.outer { height:200px; .outer { 高度:200px; width:400px;宽度:400px; border:2px dotted red;边框:2px 点红色; } }

HTML HTML

<div class="outer">
<div class="inner">
    <div class="box">
        wpfw efuiweh fuiwehf weuifh weuifh fuihwe fuihwefuiweh uwehf iuwehf uiwefhweuifh weuifhwe fuiweh fuiwehfuiwefh uiwefh weuifh weuifh weuifhwe uifh weuifhwefuiweh fuiweh fuiwehf iweufh weuih
    </div>
</div>
</div>

jQuery jQuery

$(function(){
    $(".box").css('top',-($(".box").height()/2));
    $(".box").css('left',-($(".box").width()/2));
});

Please let me know if that doesn't do what you want it to.如果这不符合您的要求,请告诉我。 You can even see on http://jsfiddle.net/mazlix/m4Ttk/2/ that resizing the inner our outer divs will still keep it centered.您甚至可以在http://jsfiddle.net/mazlix/m4Ttk/2/上看到调整内部 div 的大小仍将保持居中。

for fixed-size fits example http://jsfiddle.net/expert/vh2vy/5/对于固定尺寸适合示例http://jsfiddle.net/expert/vh2vy/5/

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

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