即使在窗口重新调整大小后(对于响应式设计),中心绝对定位DIV

编程入门 行业动态 更新时间:2024-10-24 06:26:33
本文介绍了即使在窗口重新调整大小后(对于响应式设计),中心绝对定位DIV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在屏幕中间保留一个绝对定位的DIV #box ,即使在窗口重新调整大小和所有主要设备(响应式设计)之后。响应#box是使用css3媒体查询完成的,而 left 是使用jquery计算的。但我可以把我的盒子正好在中心,在窗口大小为小屏幕有不等的左和

I want to keep an absolute positioned DIV #box at the middle of the screen even after window re-size and for all major device (responsive design). Responsive #box is done using css3 media queries while left is calculated using jquery. But I can positioned my box exactly at the center, on window re-size for small screen there is unequal left & right margins.

<HTML> <body> <div id ="box"> <a> is am athe centre?'</a> </div> </body> </HTML>

css:

#box { position:absolute; background:red; width:900px; height:400px; } @media only screen and (min-width: 979px) { #box{width:760px;background:yellow;} } @media only screen and (min-width: 768px) and (max-width: 979px) { #box{width:760px;background:pink;} } @media only screen and (min-width: 480px) and (max-width: 767px) { #box{width:460px;background:black;} } @media only screen and (min-width: 321px) and (max-width: 479px) { #box{width:300px;background:blue;} } @media only screen and (max-width: 320px) { #box{width:300px;background:grey;} }

jQuery:

function leftmargin(){ var w_box = ($('#box').width()); var w_window = $(window).width(); var w_left = (w_window - w_box)/2 ; $("#box").css("left", w_left + "px"); } $(window).resize(function() { leftmargin(); }); $(document).ready(function() { leftmargin(); });

推荐答案

css-tricks/quick-css-trick-how-to-center-an-object-exactly-in-the-center/rel =nofollow>来源),然后适应媒体查询中的 width 和 margin-left :

Center the box using CSS (source), then adapt the width and the margin-left in you media queries:

#box { position:absolute; border:1px solid red; width:760px; height:30px; background:yellow; left: 50%; margin-left:-380px; /* half of the box */ } @media only screen and (min-width: 768px) and (max-width: 979px) { #box { background:pink; } } @media only screen and (min-width: 480px) and (max-width: 767px) { #box { width:460px; background:black; margin-left:-230px; } } @media only screen and (min-width: 321px) and (max-width: 479px) { #box { width:300px; background:blue; margin-left:-150px; } } @media only screen and (max-width: 320px) { #box { background:grey; } }

在这里演示,将内框边界拖到设备大小: jsfiddle/hD657/3/

Demo here, drag the inner frame boundary to „simulate device sizes": jsfiddle/hD657/3/

基于您的JavaScript的解决方案: jsfiddle/NnDvs/

Solution based on your JavaScript: jsfiddle/NnDvs/

回答您的 comment :

margin-left 是基于框的宽度计算的,因此这是独立于任何视口或窗口宽度。我想从你的评论,你的盒子是关闭一些像素(你应该说明更清楚,所以我们可以帮助更好)。 jQuery的 width 将返回宽度without的滚动条,所以它会根据内容的高度(即如果有一个滚动条或不是)改变。您可以尝试在获取宽度之前隐藏滚动条: document.body.style.overflow =hidden; 。但一般来说,我会说,返回值 .width()应该是你正在寻找。这是您要找的吗?

margin-left is calculated based on the width of the box, so this is independent on any viewport or window width. I guess from your comment that your box is just off by some pixels (you should state that more clearly so we can help better). jQuery's width will return the width "without" the scrollbar, so it changes based on the height of the contents (i.e. if there is a scrollbar or not). You could try to "hide" the scrollbars temporary before getting the width: document.body.style.overflow = "hidden";. But in general, I would say that the returned value of .width() should be what you are looking for. Is this what you are looking for?

更多推荐

即使在窗口重新调整大小后(对于响应式设计),中心绝对定位DIV

本文发布于:2023-10-08 01:50:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小   窗口   中心   DIV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!