Javascript jquery对中divy轴错误

Javascript jquery对中divy轴错误,javascript,jquery,Javascript,Jquery,我有一些div,我想放在中间。我得到这个代码就是为了做到这一点: jQuery.fn.center = function () { this.css("position","fixed"); this.css("top", ($(window).height() / 2) - (this.outerHeight() / 2)); this.css("left", ($(window).width() / 2) - (this.outerWidth() / 2));

我有一些div,我想放在中间。我得到这个代码就是为了做到这一点:

jQuery.fn.center = function ()
{
    this.css("position","fixed");
    this.css("top", ($(window).height() / 2) - (this.outerHeight() / 2));
    this.css("left", ($(window).width() / 2) - (this.outerWidth() / 2));
    this.show(800);
    return this;
}
但问题是,当页面可以从第一个位置滚动时,div没有正确居中,它根据整个页面显示居中,而不是用户可以实际看到的屏幕部分

知道怎么修吗

JS FIDDLE:

那么试试这个

jQuery.fn.center = function ()
{
    this.css("position","fixed");
    this.css("top", ($(window).outerHeight() / 2) - (this.outerHeight() / 2));
    this.css("left", ($(window).outerWidth() / 2) - (this.outerWidth() / 2));
    this.show(800);
    return this;
}

示例:

打赌您在一个没有相对定位的div中拥有它:)好吧,div父项是“body”:o可能问题与代码中的其他内容有关:o?我检查了一些东西。加载页面时,我调用$(window).outerHeight()并警告输出它等于视口,但当函数将其冻结时,它等于整个文档的高度。请尝试向JSFIDLE提供该问题,否则没有人可以帮助:/Guess我无能为力:请同时提供html代码。创建一个