简体   繁体   English

如何将div保持在视口的中心

[英]How to keep div in the center of viewport

I am creating a feedback system for one of my projects. 我正在为我的一个项目创建一个反馈系统。 My target device is iPad. 我的目标设备是iPad。 Basically what happens is a div called into the page via ajax and it is supposed to overlay the content underneath. 基本上会发生一个通过ajax调用div的div,它应该覆盖下面的内容。 I have that part working. 我有那部分工作。

What I want to do is have the div locked to the middle of the view-port. 我想做的是将div锁定到视口的中间。 I have tried position:fixed on my element which works, except it will lock into the wrong position. 我已经尝试了position:fixed在可以正常工作的元素上,但是它将锁定在错误的位置。 It seems to be centering itself to the initial position of the viewport. 它似乎将自身居中到视口的初始位置。 If I scroll down to the bottom of a longer page and call my feedback window, it will still be near the top. 如果我向下滚动到较长页面的底部并调用我的反馈窗口,则该窗口仍将位于顶部附近。

Ajax Page (this runs when the page is called) Ajax页面(在调用页面时运行)

$(document).ready(function(){
  $(".popup").css({
    top: "50%",
    left: "50%",
    marginLeft: -$(".popup").width() / 2,
    marginTop: -$(".popup").height() / 2
  });
});

If I can find the top of the viewport I think I'd be able to get this working right. 如果我能找到视口的顶部,我想我可以使它正常工作。

I've looked into: http://www.appelsiini.net/projects/viewport but it doesn't really solve my problem. 我调查了: http : //www.appelsiini.net/projects/viewport,但这并不能真正解决我的问题。

Any help, advice or pointers in the right direction would be greatly appreciated! 任何帮助,建议或正确方向的指导将不胜感激! Thanks! 谢谢!

Fixed positioning is applied relative to the top-left corner of the window, regardless of how far down you're scrolled (which I assume is what you want). 固定位置是相对于窗口的左上角应用的, 而不管您滚动了多深 (我认为这是您想要的)。

So: 所以:

.popup {
   position:fixed;
   top:20px;
   left:40px;
   right:40px;
}

Will, first of all, put your popup 20px from the address bar (meaning, even if you scrolled to the bottom). 首先,请从地址栏中输入20像素的弹出窗口(这意味着即使您滚动到底部)。

Next, setting both left AND right will "stretch" the fixed element to start and end 40px (or whatever you give it) from both sides of the window. 接下来,同时设置leftright将“固定”固定元素从窗口的两侧开始和结束40px(或您指定的大小)。 That's a convenient way of centering this popup div. 这是居中此弹出div的便捷方法。

If your popup needs to be a fixed size – not stretched based on the width of the window – you could set both the left and right (to zero probably) and then inside this div, have another div with margin:0 auto , which will center that inner div within the fixed outer div. 如果您的弹出窗口需要固定大小-而不是根据窗口的宽度进行拉伸-您可以将左右两边(可能设置为零),然后在该div内设置另一个带有margin:0 auto div。使该内部div在固定外部div内居中。

PS Just as you can set both left and right , you can also set both top and bottom , which will have corresponding results. PS正如你可以同时设置leftright ,你还可以设置两个topbottom ,这将有相应的结果。 However, if you need a fixed height, you won't be able to vertically center it using the margin:auto trick. 但是,如果您需要固定的高度,则无法使用margin:auto技巧垂直将其居中。

不知道是不是这种情况,但是如果$(".popup")最初被display:none隐藏,那么它的宽度和高度在页面加载时将为零。

如何自动使底部居中<div>在视口上?</div><div id="text_translate"><p> 我正在创建一种仅基于纯文本、没有图像或其他任何内容的网站/实验,它仅包含一个&lt;div&gt;包含几个&lt;span&gt;和&lt;a&gt;元素,这些元素在使用 JS 点击时打开,显示堆叠的文本在&lt;div&gt;的底部。</p><p> 当屏幕上覆盖的文本过多时,问题就出现了,并且用户不断点击——因此,显示更多文本——这些新行一直堆叠在底部,不会自动可见:用户必须开始滚动到继续阅读。</p><p> 有没有办法可以自动将&lt;div&gt;的底部放在屏幕<em>的</em>中心,所以它不需要滚动? 当文本填满整个屏幕时,您是否有更好的解决方法来避免手动滚动?</p><p> 谢谢!</p><p> 编辑:我添加了一段代码,以便更好地理解我所做的。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;div id="container"&gt; &lt;p class="fade-in"&gt; &lt;a data-opens="1" href="#"&gt;Hello&lt;/a&gt;.&lt;span data-openedby="1" class="fade-in"&gt; How are &lt;a data-opens="2" href="#"&gt;you&lt;/a&gt;? &lt;/span&gt;&lt;span class="fade-in" data-openedby="2"&gt;&lt;a data-opens="3" href="#"&gt;Welcome&lt;/a&gt; to my website.&lt;/span&gt; &lt;/p&gt; &lt;/div&gt;</pre></div></div><p></p><p> 基本上,这段代码,但不是只有三个&lt;a&gt; ,到目前为止,它有 300 个带有各自的&lt;span&gt; 。</p></div> - How can I automatically center the bottom of a <div> on the viewport?

暂无
暂无

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

相关问题 如何判断div的中心是否在视口中? - How do I tell if the center of a div is in the viewport? 在视口中将点/格居中 - Center a point/div in viewport 将div保持为视口中心 - keep div centered to viewport 从视口中心展开div - Expand div from center of viewport 如何将div居中于Google等浏览器视口的中间 - how to center div in middle of browser viewport like google 如何自动使底部居中<div>在视口上?</div><div id="text_translate"><p> 我正在创建一种仅基于纯文本、没有图像或其他任何内容的网站/实验,它仅包含一个&lt;div&gt;包含几个&lt;span&gt;和&lt;a&gt;元素,这些元素在使用 JS 点击时打开,显示堆叠的文本在&lt;div&gt;的底部。</p><p> 当屏幕上覆盖的文本过多时,问题就出现了,并且用户不断点击——因此,显示更多文本——这些新行一直堆叠在底部,不会自动可见:用户必须开始滚动到继续阅读。</p><p> 有没有办法可以自动将&lt;div&gt;的底部放在屏幕<em>的</em>中心,所以它不需要滚动? 当文本填满整个屏幕时,您是否有更好的解决方法来避免手动滚动?</p><p> 谢谢!</p><p> 编辑:我添加了一段代码,以便更好地理解我所做的。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;div id="container"&gt; &lt;p class="fade-in"&gt; &lt;a data-opens="1" href="#"&gt;Hello&lt;/a&gt;.&lt;span data-openedby="1" class="fade-in"&gt; How are &lt;a data-opens="2" href="#"&gt;you&lt;/a&gt;? &lt;/span&gt;&lt;span class="fade-in" data-openedby="2"&gt;&lt;a data-opens="3" href="#"&gt;Welcome&lt;/a&gt; to my website.&lt;/span&gt; &lt;/p&gt; &lt;/div&gt;</pre></div></div><p></p><p> 基本上,这段代码,但不是只有三个&lt;a&gt; ,到目前为止,它有 300 个带有各自的&lt;span&gt; 。</p></div> - How can I automatically center the bottom of a <div> on the viewport? 如何在页面中央保留div? - How do I keep a div in the center of the page? 将div从父div扩展到中心视口 - Expand div from parent div to center viewport 如何将元素保留在视口中? - How to keep element in viewport? 将body / div的背景位置居中到视口的中间 - Center the background position of body / div to middle of Viewport
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM