简体   繁体   English

将div保持为视口中心

[英]keep div centered to viewport

I have a form within a container, when the user resizes the window I need to the form to move into the center of the container, and when he goes full screen it should return to the original position. 我在容器中有一个表单,当用户调整窗口大小时我需要将表单移动到容器的中心,当他全屏显示它应该返回到原始位置。

The problem is the form starts aligned to the right edge of the container. 问题是表单开始对齐容器的右边缘。 See the screenshot below. 请参见下面的截图。 As you can see I need a smooth form transition so that it moves along the y when the viewport is resized. 正如您所看到的,我需要一个平滑的表单转换,以便在调整视口大小时沿y移动。

How can i accomplish this solution with either javascript or css? 如何使用javascript或css完成此解决方案?

在此输入图像描述

https://jsfiddle.net/ns5xxnxs/6/ https://jsfiddle.net/ns5xxnxs/6/

<div id = "slider">
    <div class = "container">
        <div id = "form" class="col-sm-3 col-sm-offset-8 col-md-3">
        </div>

       </div>
    </div>

Edward Hi there. 爱德华你好。
It sound like you are just after a transition to work here when you resize the window. 当你调整窗口大小时,听起来就像你刚刚过渡到这里工作一样。 And not centering the form div. 而不是以表格div为中心。 because in your fiddle you seem to have that working for how you want it. 因为在你的小提琴中你似乎有你的工作方式。

So to get the transition when resized... You need to have the transition go from one size to another different size. 因此,要在调整大小时进行转换...您需要将转换从一种尺寸转换为另一种尺寸。
To do this lets use the col-xx-xx classes to have the transition go from one to the other. 为此,可以使用col-xx-xx类将转换从一个转换为另一个。

I added in the css a class of transition-with . 我在css中添加了一个转换 class

Have a look at the code and for how the container > row > div are used and now where your slider id is. 查看代码以及如何使用容器> row> div以及现在滑块 id位置。

Here is the working Fiddle , just move the inner frame on the left to resize. 这是工作小提琴 ,只需移动左边的内框来调整大小。

(Note)... I use all in the transition: all 2s ease-out; (注)......我在transition: all 2s ease-out;使用全部 transition: all 2s ease-out; rather than using width , because all will animate the div on both sides. 而不是使用width ,因为所有都将动画两侧的div。 If I just used width it will only expand from one side. 如果我只是使用width它只会从一侧扩展。 Try both to see what I mean here. 试着看看我的意思。

Hope this gets you started. 希望这能让你开始。
CSS CSS

#form{
    height:250px;
    background-color:red;
}
#slider{
    background-color:grey;
}        
.transition-width { 
    transition: all 2s ease-out; 
    webkit-transition: all 2s ease-out; 
} 

HTML HTML

<div class="container">
    <h3>Transition happens when window resized.</h3>
    <div class="row" id="slider">
        <div id="form" class="col-md-3 col-md-offset-8 col-sm-8 col-sm-offset-2 col-xs-10 col-xs-offset-1 transition-width">
        </div>
    </div>
</div> <!--end container-->  

在此输入图像描述

Try this out - https://jsfiddle.net/ns5xxnxs/8/ 试试这个 - https://jsfiddle.net/ns5xxnxs/8/

Bootstrap css was forcing it towards right. Bootstrap css迫使它走向正确。 I have changed below thinks to make sure it flows in main container. 我在下面改变了认为确保它在主容器中流动。 Changes are below. 变化如下。 Hope this helps. 希望这可以帮助。

#form
{
    height:250px;
    background-color:red;
    margin-left:0px!important;
    width:100%!important;

}

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

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