简体   繁体   English

获取位置:相对宽度:100%divs彼此相邻浮动

[英]Get position:relative width:100% divs to float next to each other

I have a container which is 100% width (thus whatever width the viewport happens to be), and inside it I have 3 child divs which are also 100% width (so they fill the container which fills the viewport), I want to be able to slide a div into the center (so it's visible in the viewport and not outside) when a corresponding link is clicked (I'm actually using twitter bootstrap 3.0 tabs for this, but I'm overriding the default fade-in/fade-out behaviour in favour of creating my own slide-in/slide-out behaviour). 我有一个宽度为100%的容器(因此视口正好是宽度),而且里面有3个子div,也是100%宽度(所以它们填充了填充视口的容器),我想成为当点击相应的链接时,我可以将div滑入中心(因此它在视口中可见,而不是在视口中可见)(我实际上使用的是twitter bootstrap 3.0标签,但我正在覆盖默认的淡入淡出/淡入淡出 - 支持创建我自己的滑入/滑出行为的行为)。

I'm currently managing this with position:absolute on the 3 divs and then keeping 2 outside the container (to the left and right) using negative left positioning (eg left:-100%/left:100%) which works just fine, the only issue is I need the 3 divs to be position:relative so they stay inside the document flow, since they're also all of a dynamic height (on each page load, and then they grow as successive data is loaded into them on vertical page scroll) which has caused me to hit a wall. 我现在用位置管理它:绝对在3个div上,然后使用负左定位(例如左:-100%/左:100%)保持2在容器外(向左和向右)工作得很好,唯一的问题是我需要3个div来定位:相对所以他们留在文档流中,因为它们也都是动态高度(在每个页面加载时,然后它们随着连续数据加载到它们中而增长垂直页面滚动)这让我撞墙了。

TLDR: I need 3 position:relative divs which are 100% width to all have the same top value and be next to each other as if they were float:left (though 2 will always be outside their container div which is also 100% width) TLDR:我需要3个位置:100%宽度的相对div都具有相同的顶值,并且彼此相邻,好像它们是浮动的一样:left(尽管2总是在它们的容器div之外,也是100%宽度)

There might not be an appropriate solution, but I thought it was worth a shot to ask. 可能没有合适的解决方案,但我认为值得一试。

I had a similar problem some days ago. 几天前我遇到了类似的问题。 My approach was as follows: 我的方法如下:

Markup: 标记:

<div class="outer">
    <div class="inner"></div>
    <div class="inner"></div>
    <div class="inner"></div>
</div>

CSS: CSS:

.outer {
    width: 300%;
    margin-left: -100%;
}
.inner {
    width: 33.33333%;
    float: left;
}

With this code the middle inner div is visible in the viewport. 使用此代码,中间内部div在视口中可见。 All three inner divs are floating next to each other. 所有三个内部div都彼此相邻。 So if you want to get the first or the third inner div into the viewport you just have to set the margin-left of the outer div to 0% / -200% using Javascript. 因此,如果您想要将第一个或第三个内部div放入视口中,您只需使用Javascript将外部div的margin-left设置为0%/ -200%。

If you want to have a slide effect you have to add a CSS transition to the .outer class for the margin-left. 如果你想要一个幻灯片效果,你必须为.outer类添加一个CSS转换为margin-left。

I used this approach for a mobile website with off-canvas menus, and it works pretty well for me. 我将这种方法用于带有画布菜单的移动网站,它对我来说效果很好。 I hope this helps!! 我希望这有帮助!!

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

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