简体   繁体   English

如何在响应式布局中将按钮放置在没有固定高度和位置的区域中:绝对? (UPDATE)

[英]How do I position a button in a responsive layout, in an area that has no fixed height and position:absolute? (UPDATE)

Here's a UPDATED fiddle with my layout: http://jsfiddle.net/RDmN4/24/ 这是我布局的更新小提琴: http : //jsfiddle.net/RDmN4/24/

HTML: HTML:

<div id="container">
<div id="header">header goes here</div>
</div>
<div id="flexible_height">
<!-- what i can't figure out -->
<a class="button" href="#" title="">button</a>    
</div>
<div id="test">
<p>lorem ipsum, 2 or 3 paragraphs go here</p>
</div>
<div id="footer">footer goes here</div>

CSS: CSS:

#container
{
width:100%; max-width:100%;
position:absolute; z-index:1;
bottom:28px; top:0; left:0;  
background:grey; /* just to identify area */       
}
#header
{
height:42px;
background:green; /* just to identify area */    
}
#footer
{
width:100%; max-width:100%;
position:absolute; z-index:2;
bottom:0; left:0;   
height:28px;
background:blue; /* just to identify area */   
}
#test
{
width:100%; max-width:100%;
position:absolute; z-index:2;
bottom:28px; left:0;
background:white; /* just to identify area */
}
#flexible_height
{
position:absolute; top:42px; left:0;
z-index:2;
width:100%;    
}
.button
{
width:100px; padding:10px 0; text-align:center; display:block;
background: red; /* just to identify button */    
}

The button should be positioned in the center of the grey area (container) horizontally and vertically but also remain there when the resolution changes and the white area (test) changes the height (which is not fixed). 该按钮应水平和垂直放置在灰色区域(容器)的中心,但是当分辨率改变而白色区域(测试)改变高度(不固定)时,也应保持在该位置。 I just can't figure out how to keep that button between the header and the test div even if the width and the height of the viewport changes (yes, it's a layout for a mobile website) ... I assume there should be a way to do it, probably with javascript, but i don't know how to do it ... Any help would be appreciated! 即使视口的宽度和高度发生变化,我也无法弄清楚如何在标题和测试div之间保持该按钮(是的,这是移动网站的布局)...我认为应该有一个做到的方式,可能与javascript,但我不知道该怎么做...任何帮助将不胜感激!

PS: I can't modify the html structure in any way. PS:我无法以任何方式修改html结构。 i can only move the button around, in and out of divs. 我只能在div内外移动按钮。

EDIT : I created a new div - #flexible_height. 编辑 :我创建了一个新的 div-#flexible_height。 Now that the button is inside that div I can use position:absolute; 现在按钮在该div内,我可以使用position:absolute;。 top:0; 顶部:0; left:0; 左:0; right:0; 右:0; bottom:0 on it ONLY IF somehow I calculate the dynamic height of the div . 仅当我以某种方式计算div动态高度时,它的bottom:0才可以。 I am new to javascript and haven't figured out a way to do it. 我是javascript新手,还没有想办法。 Any ideas? 有任何想法吗? Thanks! 谢谢!

Hm. 嗯。 It's not easy. 这并不容易。

You can sent style to button: 您可以将样式发送到按钮:

button {
  display: block;
  margin: 0 auto;
  top: 50%;
  left: 50%;
  position: absolute;
  margin-top: /* size of top container*/;
}

And when someone change window size, or rotate screen, you can use: 当有人更改窗口大小或旋转屏幕时,您可以使用:

window.onresize = function() {
    var container = document.querySelector('#container');
    var test = document.querySelector('#test');
    var newwidth = container.offsetWidth-test.offsetWidth;
    var button = document.querySelector('.button');
    button.style.marginTop = newwidth;
}

(It's just sample of code and point, so edit it for your case, of course) (这只是代码和要点的示例,因此请根据您的情况进行编辑)

When We make a responsive site that time we use "Media Query" Like 那个时候当我们制作一个响应式网站时,我们使用“媒体查询”

@media only screen and (min-width: 768px) and (max-width:1024px){
  .XYZ{}
}
@media only screen and (min-width: 480px) and (max-width: 767px){
  .XYZ{}
}
@media only screen and (min-width: 320px) and (max-width: 479px){
  .XYZ{}
}

In your case you want to button set in mobile view. 您要在移动视图中设置按钮。

@media only screen and (min-width: 480px) and (max-width: 767px){
  .button{width:100%;}
}

@media only screen and (min-width: 320px) and (max-width: 479px){
  .button{width:100%;}
}

I don't know why you have used absolute position at various places. 我不知道你为什么在各个地方都使用绝对位置。 . you have made you your CSS complicated for your layout.. You should use css3 media queries or javascript But if you can't change your css you can use this tutorial for centering .. 您已使CSS的布局变得复杂。.您应该使用css3媒体查询或javascript,但是如果您无法更改css,则可以使用本教程进行居中..

It will help you.. 会帮你的..

http://tutorialzine.com/2010/03/centering-div-vertically-and-horizontally/ http://tutorialzine.com/2010/03/centering-div-vertically-and-horizo​​ntally/

您可以为类按钮.button {margin:0 auto}添加一条CSS行,使其成为页面的中心

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

相关问题 表格布局固定-当位置绝对时 - table layout fixed - when th position is absolute 位置问题:固定和响应/自适应布局 - Problems with position:fixed and responsive / adaptive Layout Javascript拖放:当可拖动对象具有固定或绝对位置时,它会在可放置区域后面消失 - Javascript drag and drop: when draggable object has a fixed or absolute position it disappears behind droppable area 固定位置的绝对效果 - Absolute effect in fixed position 如何将元素从静态布局位置动画到绝对布局位置? - How can I animate an element from a static layout position to an absolute layout position? 如何使图像部分滚动? 位置:固定和位置:绝对之间的一半 - How do you make an image scroll partly? Halfway between position:fixed and position:absolute 当我的整个页面都具有“ position:absolute”时,如何检查我是否位于顶部? - When my whole page has 'position: absolute', how do I check if I am at the top or not? 位置:固定和绝对同时。 怎么样? - position: fixed and absolute at the same time. HOW? 我如何使用绝对位置子元素获得高度响应 - How can I have a responsive height with absolutley position child elements 如何将 HTML 元素定位在具有不同高度的固定 div 下? - How do I position an HTML element under a fixed div with variant height?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM