修正了middel中的位置div

时间:2015-09-10 06:48:47

标签: javascript jquery html css css3

创建了一个固定位置的div弹出窗口。这个div包括表单元素。如果屏幕尺寸很小,那么弹出窗口会从底部剪切,我们可以滚动浏览器推车。

我添加了解决方案,以便从底层开始

.popup {
    bottom: 15%;
    position: fixed;
}

但如果屏幕尺寸很大,它会位于底部并且不会显示在中心位置。

我正在寻找类似弹出窗口的解决方案应该始终位于页面的中心,如果屏幕尺寸很小,那么它应该从底部开始。不应该从底部切割。

2 个答案:

答案 0 :(得分:0)

这是我最常使用的自定义自适应弹出窗口。为了使div居中,我使用JQuery高度,我的脚本计算设备窗口高度并相应地调整弹出窗口。

根据我的说法,这是创建popups

最简单,最棒的方式

我希望它对你有所帮助。

JSFiddle : Demo

<强> HTML

<div class="popup"> 
    <span class="p_box">
        <span class="close">X</span>
        <h2 id="popup_head"> Popup Alert !</h2>
        <p> This is popup content. The dummy text is here.</p>
        Some more dummy text. 
    </span>
</div>
<div class="content">
    <h1>Hello There...</h1>
    <p>This is just a dummy text.</p>
    <p>This is just a dummy text.</p>
    <p>This is just a dummy text.</p>
    </br>
    <h2>Content</h2>
    <p>
        Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then
    </p>

    <button id="popup_bt">Popup</button>
</div>

<强> CSS

body {
    margin:0px;
    width:100%;
}
.content {
    padding:55px;
    text-align:justify;
}
.popup {
    display:block;
    visibility:hidden;
    position:fixed;
    top:0px;
    left:0px;
    min-width:100%;
    min-height:100%;
    background:rgba(0, 0, 0, 0.7);
    z-index:9999;
    overflow:hidden;
    text-align:center;
}
@media(max-width: 1020px) {
    .p_box {
        width:30%;
    }
}
@media(max-width: 800px) {
    .p_box {
        width:40%;
    }
}
@media(max-width: 640px) {
    .p_box {
        width:60%;
    }
}
@media(max-width: 420px) {
    .p_box {
        width:80%;
    }
}
.p_box {
    position:relative;
    margin:0px auto;
    display:block;
    height:auto;
    width:25%;
    padding:20px;
    background:white;
    text-align:left;
}
.close {
    display:inline;
    position:absolute;
    right:0px;
    top:5px;
    margin-right:5px;
    background:#E32222;
    color:white;
    height:20px;
    width:20px;
    border-radius:5px;
    z-index:9999;
    cursor:pointer;
    text-align:center;
}
#popup_head {
    color:#E32222;
    text-align:center;
}

<强> JQuery的/ JavaScript的

$(document).ready(function(){

        var dh = window.innerHeight;
        var pbox_h = $(".p_box").innerHeight();
        var mid_scr = dh/2;
        var mid_box = pbox_h/2;
        var topPos = mid_scr - mid_box;
        $(".p_box").css("top","" + topPos + "px");

    $("#popup_bt").click(function(e){
        $(".popup").fadeIn();
        $(".popup").css("visibility","visible");
    });
    $(".close").click(function(event){
        $(".popup").fadeOut();
    });
});
  

注意:您可以自定义弹出框,我添加了一些虚拟内容。

答案 1 :(得分:0)

中心事物是小菜一碟:

.centereddiv {position:absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);}

希望这有帮助!