简体   繁体   English

如何使视口垂直居中?

[英]How to make a vertically center of viewport?

I explain my problem: 我解释我的问题:

I have created an overlay, there is a gallery of thumbnail images and clicking on a picture, it will appear in a box with the image clicked to larger (the size fit the width of the window). 我创建了一个叠加层,这里有一个缩略图图像库,然后单击图片,它将显示在一个框中,单击该图像会放大(尺寸适合窗口的宽度)。 This box is perfectly aligned horizontally and alignment remains even if you resize the window. 此框在水平方向上完美对齐,即使您调整窗口大小,对齐也会保持。

The problem is that I want a vertical alignment of the box. 问题是我想要盒子垂直对齐。 Aligned in the viewport, because I wish it was immediately visible. 在视口中对齐,因为我希望它可以立即可见。 But I do not want during the scroll, the box does not move together with the page. 但是我不希望在滚动过程中,框不随页面一起移动。 (therefore be excluded display: fixed ) (因此,排除显示:固定

Now the box is so: 现在盒子是这样的:

#box {
 padding: 0 5px 10px;
 background-color:#a0a0a0;
 margin: 5px 5px 15px 5px;
 display:none;
 z-index:+300;
 position:absolute;
 left:50%;
 top:20%;
 border-radius: 10px;
}

The div #box before that image is clicked, has display:none . 单击该图像之前的div #box显示为:none So the alignment in the viewport would only during the appearance of the box. 因此,仅在框的外观期间在视口中对齐。 When he moves the page (scroll) alignment does not matter. 当他移动页面(滚动)时,对齐方式无关紧要。

This is jsfiddle: http://jsfiddle.net/tvafw5Ls/13/ 这是jsfiddle: http : //jsfiddle.net/tvafw5Ls/13/

If is possible to do this only with CSS3 is better, but also solution with js is ok. 如果可能的话,仅使用CSS3会更好,但使用js解决方案也可以。

I hope that can you help me! 希望您能帮我! Thanks a lot! 非常感谢!

Alright, here is your JsFiddle updated with the viewport centering - I adjusted this part of your code to look like so: 好的,这是您的JsFiddle更新为视口居中的方式-我将代码的这一部分调整为如下所示:

if (widthimage < i2.width) {
        $('#box').css('left', '50%');
        $('#box').css('top', '50%');
        $('#box').css('transform', 'translate(-50%,-50%)');
        $('#box').css('-webkit-transform', 'translate(-50%,-50%)');
        $("#immagine img:last-child").css('width', '100%');
    } else {
        $('#box').css('left', '50%');
        $('#box').css('top', '50%');
        $("#immagine img:last-child").css('width', '');
        $('#box').css('transform', 'translate(-50%,-50%)');
        $('#box').css('-webkit-transform', 'translate(-50%,-50%)');
    }


    $(window).resize(function () {
        $('#box').css('left', '0%');
        $("#immagine img:last-child").css('width', '100%');
        var widthimage = $("#immagine img:last-child").width();
        if (widthimage < i2.width) {
            $('#box').css('left', '50%');
            $('#box').css('top', '50%');
            $('#box').css('transform', 'translate(-50%,-50%)');
            $('#box').css('-webkit-transform', 'translate(-50%,-50%)');
            $("#immagine img:last-child").css('width', '100%');

        } else {
            $('#box').css('left', '50%');
            $('#box').css('top', '50%');
            $("#immagine img:last-child").css('width', '');
            $('#box').css('transform', 'translate(-50%,-50%)');
            $('#box').css('-webkit-transform', 'translate(-50%,-50%)');

        }

    });

What this is doing, is a nifty css trick that looks like this: 这是一个漂亮的CSS技巧,看起来像这样:

position: absolute;
top: 50%;
left: 50%;
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);

This will vertically and horizontally center anything in CSS. 这将使CSS中的所有内容垂直和水平居中。

Here is the JsFiddle - https://jsfiddle.net/rockmandew/tvafw5Ls/17/ 这是JsFiddle- https: //jsfiddle.net/rockmandew/tvafw5Ls/17/

I will edit my answer to include how to make the lightbox stay fixed when scroll (if I understand your question correctly, you want the lightbox to stay in a fixed position if the user scrolls, but for it to center if the window/viewport is resized.) 我将编辑答案,以包括如何使灯箱在滚动时保持固定(如果我正确理解了您的问题,如果用户滚动,则希望灯箱保持在固定位置,但如果窗口/视口处于调整大小。)

** I lied, my current solution will make the lightbox stay in the initial position when scrolling - if you wanted it to follow the page on scroll you would add this to your code: **我撒谎了,我目前的解决方案是在滚动时让灯箱保持在初始位置-如果您希望它跟随滚动页面,您可以将其添加到代码中:

$(window).scroll(function() {
  $( '#box' ).css('position', 'fixed');
});

** UPDATE ** I have updated my code to include lorem ipsum at the bottom to provide a scroll - test this link, if "it doesn't work" then your question is wrong. **更新**我已经更新了我的代码,在底部包括lorem ipsum,以提供滚动显示-测试此链接,如果“它不起作用”,那么您的问题是错误的。 https://jsfiddle.net/rockmandew/tvafw5Ls/18/ https://jsfiddle.net/rockmandew/tvafw5Ls/18/

Using your JS, I've come up with This Fiddle 使用您的JS,我想出了这个小提琴

What I've done is changed your initial #box css to this: 我所做的就是将您最初的#box CSS更改为:

#box {
    width:90%;
    max-width:775px;
    padding: 0 5px 10px;
    background-color:#a0a0a0;
    margin: 5px 5px 15px 5px;
    display:none;
    z-index:+300;
    position:fixed;
    left:50%;
    top:50%;
    transform: translate(-50%, -50%);
    border-radius: 10px;
}
#box #immagine img {
    width:100%;
}

Notice how I've changed the box to initially have the fixed position. 请注意如何我已经改变了框最初有fixed位置。 Then I've placed it in the dead center of the window using translate-negative-position. 然后,我将其使用translate-negative-position放置在窗口的死点中。 I've also given the box a width and max-width so your js doesn't need to handle that. 我也给了盒子一个width和max-width,所以你的js不需要处理。

After that, I've done a few things to your JS. 之后,我对您的JS做了一些事情。 I removed the CSS you've given the box with the translate3d, then I've found the initial position of the fixed box, changed the position to absolute and given the height of the box position. 我删除了您为带有translate3d的框指定的CSS,然后找到了固定框的初始位置,将位置更改为absolute位置并指定了框位置的高度。

if (widthimage < i2.width) {
    //Took off the translates from here
    $('#box').css('left', '0%');
    $("#immagine img:last-child").css('width', '100%');
} else {
    //and here
    $('#box').css('left', '50%');
    $("#immagine img:last-child").css('width', '');
}

//Added this to the mix to make the box position absolute again
var imgOffsetTop = $('#box').position().top;
var boxHeight = $('#box').outerHeight();
$('#box').css({
    "position":"absolute",
    "top":(imgOffsetTop+(boxHeight/2))+"px"
});

UPDATE I've turned the fixed-to-absolute solution into a function and have made it run on load and window resize. 更新我已经将固定的绝对解决方案变成了一个函数,并使其在加载和调整窗口大小时运行。 I've also edited a bit more of your CSS, making your #box have a max-width and width. 我还编辑了一些CSS,使您的#box具有最大宽度和最大宽度。 Take a look at the fiddle for the example. 看一下这个例子的小提琴。

Pure CSS way, simulating a table and a cell, works nice in every modern browser. 纯CSS方式(模拟表和单元格)在每种现代浏览器中均能很好地工作。 To just give you the base idea. 只是给你基本的想法。 http://jsfiddle.net/q8bevnL2/ http://jsfiddle.net/q8bevnL2/

<style>
#box{
    position:fixed;
    width:100%;
    height:100%;
    background:black;
    color:white;
    text-align:center;
    display:table;
}

#content{   
    display:table-cell;
    vertical-align: middle;
}
</style>

<div id="box">
    <div id="content">My stuff</div>   
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM