jquery弹出窗口找到真正的顶部

标签 jquery popup height

我有一个 jquery 弹出脚本,为了使弹出窗口在屏幕上居中,代码如下:

function centerPopup(){
  var windowWidth = document.documentElement.clientWidth;
  var windowHeight = document.documentElement.clientHeight;
  var popupHeight = $(".popupContent").height();
  var popupWidth = $(".popupContent").width();

  $(".popupContent").css({
    "position": "fixed",
    "top": windowHeight/2-popupHeight/2,
    "left": windowWidth/2-popupWidth/2
    });
  }

这有效,除非我需要激活 div 标签内的弹出窗口。它看起来更靠下,有时甚至脱离了页面。我假设它认为 div 标签的顶部是页面顶部。

我怎样才能解决这个问题并让它找到实际的主页顶部?

最佳答案

您可以尝试将此元素在浏览器中居中

$.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;
}


$(".popupContent").center();

这是一个example .

更新: example .

关于jquery弹出窗口找到真正的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9838939/

相关文章:

css - 如何让 div 高度自动调整为背景大小?

javascript - Cufon 元素在 IE 上的褪色问题

php - 将jquery mobile和php mysql转为apk文件

ANDROID:如何从所有窗口顶部的通知或长按搜索按钮启动弹出对话框?

javascript - 在弹出窗口中打开 href 链接

javascript - iframe 高度为 iphone 中容器的 100%。不可能吗?

jquery - 使用Jquery,如何抓取类为 'c1'并且href中有abcdef的所有链接

javascript - 多次打开弹出窗口无法关闭

c# - 这种效果叫什么,C# 有它吗?

css - 高度:自动不适用于当前的CSS