【问题标题】:Internet Explorer Centering DIV issueInternet Explorer 居中 DIV 问题
【发布时间】:2013-09-27 16:20:53
【问题描述】:

我得到了一些将 div 置于网页中间的代码。该代码在 chrome 上运行良好,但由于某种原因,在 IE(我认为是 9 或 10)上它第一次运行,然后就不行了。

为了演示这个问题,这里有一些照片:(忽略你不懂的标志,那是我的语言)

铬:

IE第一次点击对象打开:

IE第二次或更多次,当我点击一个对象打开时:

这是我用来使 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));
    return this;
}

使用center()函数的代码:

$('#products,#search-result').on("click",'.product-item-info',function() {
    var pid = $(this).find('input').val();
    $.get('product_info.php',{pid:pid},function(data){
        $('#product-lb').html(data).append('<span class="x"><span>X</span></span>');
        $('.x').click(function() {
            $('#dimmer').click();
        });

        $('#dimmer').css({width:$('html').width(),height:$('html').height()});
        $('#product-lb').center();
        $('#product-lb').show(800);
        $('#dimmer').show();
        $(window).resize(function() {
            $('#product-lb').center();
        });
    });
});

解释: #products 是包含所有蓝色和绿色彩色 div 的 div,如图所示 .product-item-info 是可以在每个产品 DIV 中找到的矩形类。 #dimmer 是黑色背景 #product-lb 是必须居中显示在页面上的 div

我希望这是足够的信息!如果您需要更多,请随时询问。

提前致谢!

【问题讨论】:

  • 你能搞定这个或提供你网站的链接吗?
  • @GaneshPandhere 我将发布一个演示网站的链接。由于某种原因,我的主人此刻遇到了困难,所以我需要再等几分钟。
  • @GaneshPandhere 不幸的是,我的共享 IP 地址似乎受到 DDOS 攻击,因此,共享 IP 地址正在被过滤,这需要一段时间,因为我将能够发布链接在这里。
  • 我认为您需要将+ "px" 添加到顶部和左侧css 位置的末尾-this.css("top", ($(window).height() / 2) - (this.outerHeight() / 2) + "px");
  • @Pete 按照你的逻辑,为什么它在第一次尝试时就成功了?顺便说一句,它没有工作

标签: javascript jquery internet-explorer


【解决方案1】:

如果您已经在使用 jQuery,为什么不使用jQueryUI 作为对话框?

无论如何,如果你想走正常的路why not use the awesome position:absolute method?

这也适用于 div 并且与 IE7+ 兼容,此外它还只是 CSS 的东西。

【讨论】:

  • div 高度未知。因此,必须使用固定位置或绝对位置。我不想为此目的使用 jQueryUI,因为没有理由使用它
猜你喜欢
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
相关资源
最近更新 更多