【问题标题】:Center a popup div居中弹出 div
【发布时间】:2014-06-15 22:42:17
【问题描述】:

我正在为我的网站制作一个自定义图库,并且我已经将概述部分与所有缩略图一起使用。单击缩略图时,屏幕上会显示完整图片,但我无法将其与屏幕中心对齐。

请注意,每张图片都有不同的尺寸。

这是我到目前为止所做的:

.overlay{
    cursor: pointer;
    display: none;
    position: fixed;
    top: 0%;
    left: 0%;
    width: 100%;
    height: 100%;
    background-color: black;
    z-index:1001;
    -moz-opacity: 0.8;
    opacity:.80;
    filter: alpha(opacity=80);
}
.popup {
    display: none;
    position: absolute;
    margin: 0 auto;
    border: 8px solid orange;
    background-color: #eee;
    z-index:1002;
    overflow: auto;
}
.galleryentity{
    display: table;
    margin-bottom: 35px;
    float: left;
    width: 259px;
    height: 250px;
    background-color: #2B3039;
    margin-right: 30px;
    position: relative;
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
    -webkit-box-shadow: 0px 3px 5px 2px rgba(50, 50, 50, 0.75);
    -moz-box-shadow:    0px 3px 5px 2px rgba(50, 50, 50, 0.75);
    box-shadow:         0px 3px 5px 2px rgba(50, 50, 50, 0.75);
}
.gallerylink{
    display: block;
    height: 100%;
    width: 100%;
    text-decoration: none;
}

这里是 HTML:

<div class="galleryarea">
    <div class="galleryframe">
        <div class="galleryentity" style="background-image:url('pic1.jpg');">
            <a href="javascript:void(0)" class="gallerylink" onclick = "document.getElementById('pic1').style.display='block';document.getElementById('fade').style.display='block'"></a>
            <div id="pic1" class="white_content"><img class="pic1.jpg"></div>
        </div>
        .
        .
        .
    </div>
</div>

谢谢。

【问题讨论】:

  • 如果你使用 jQuery 并且如果你有不同尺寸的图像,你为什么不试试 jQuery 位置选项:jqueryui.com/position。您可以相对于屏幕或任何元素定位元素,左、中、右、下、上等。试一试
  • 嗯,看起来很复杂……我试试
  • 嗯,这并不复杂。文档使它变得复杂。试试这个链接:css-tricks.com/jquery-ui-position-function。就像简单的英语一样简单!
  • 不要使用 0% ,0 就够了。
  • @KamleshKushwaha 好的,这样吗? $(".popup").position({ "my": "center center", "at": "center center", "of": $("body") });

标签: html css popup alignment center


【解决方案1】:

居中弹出窗口的 CSS 方法:

.popup {

      position:fixed;
      /*display: none;*/    /* debug */
      border: 2px solid orange;  /* 8 is too thick ? */
      background-color: #eee;
      z-index:1002;
      overflow: auto;
      top:50%;
      left:50%;
      width:500px;  /* adjust accordingly */
      height:500px;   /* adjust accordingly */
      margin-left:-250px;   /* negative half of width above */
      margin-top:-250px;   /* negative half of height above */

       }

【讨论】:

  • 这在我的情况下不起作用,因为每张图片都有不同的尺寸。无论如何,谢谢。
  • 为什么溢出:自动呢?无论如何,对于横向居中,您可以使用 margin: 0 auto;
  • 哦,只是过去代码的一些残余,我忘了删除 :)
【解决方案2】:

我猜,在你的情况下你必须使用body: 演示:http://jsfiddle.net/lotusgodkk/GCu2D/177/

JS:

$(document).ready(function () {
    $(".checked").position({
        "my": "center",
        "at": "center",
        "of": 'body' //Or window
    });
});

【讨论】:

  • 好吧,代码“做了”一些事情,但它的行为很奇怪。如果我按照您的方式放置该代码,则图片会在屏幕的右下角显示它的左上角。我做错了什么?
  • 你能为它创建一个小提琴吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
  • 2015-05-04
相关资源
最近更新 更多