简体   繁体   English

空格:nowrap似乎会影响内部元素的位置:绝对

[英]white-space: nowrap seems to affect inner elements position: absolute

I want to have a horizontal list of items, contained in a horizontally scrollable outer-wrapper with a fixed width. 我想有一个水平的项目列表,包含在具有固定宽度的水平可滚动外部包装器中。 The outer-wrapper has a relative position. 外包装纸具有相对位置。 One of the items contains an absolute positioned div. 其中一项包含绝对定位的div。

When scrolling the outer-wrapper, I was expecting the green overlay to remain at the same position. 当滚动外包装纸时,我期望绿色的覆盖物保持在同一位置。 I thought position: absolute is always relative to the first ancestor with a defined position (which would be the outer-wrapper)? 我认为位置:绝对总是相对于具有定义位置的第一个祖先(这将是外包装)?

I am using white-space: nowrap for the #wrapper to get the items in a row. 我正在使用空格:nowrap用于#wrapper来连续获取项目。

 #outer-wrapper { position: relative; width: 300px; height: 150px; overflow: scroll; } #wrapper { white-space: nowrap; display: inline-block; } #one { background-color: red; } #two { background-color: blue; } .box { white-space: normal; display: inline-block; width: 200px; height: 150px; } .overlay { background-color: green; position: absolute; bottom: 0px; left: 0px; width: 100%; height: 40px; } 
 <div id="outer-wrapper"> <div id="wrapper"> <div id="one" class="box"> <div class="overlay"></div> </div> <div id="two" class="box"></div> </div> </div> 

I'd like the markup to remain as it is in the example, although this is not totally fixed. 我希望标记可以保留在示例中,尽管这不是完全固定的。

And I can't really define a fixed width for the horizontal list. 而且我真的不能为水平列表定义固定宽度。

If I've got your question correctly then it is not possible with only HTML and CSS. 如果我正确回答了您的问题,那么仅HTML和CSS是不可能的。 However you can achieve it with jQuery as follows: 但是,您可以使用jQuery如下实现:

 $(function() { var wrapper = $('#outer-wrapper'); $('#outer-wrapper').scroll(function() { $('.overlay').css({ 'marginLeft': wrapper.scrollLeft() }); }); }); 
 #outer-wrapper { position: relative; width: 300px; height: 150px; overflow: scroll; } #wrapper { white-space: nowrap; display: inline-block; } #one { background-color: red; } #two { background-color: blue; } .box { white-space: normal; display: inline-block; width: 200px; height: 150px; } .overlay { background-color: green; position: absolute; bottom: 0px; left: 0px; width: 100%; height: 40px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="outer-wrapper"> <div id="wrapper"> <div id="one" class="box"> <div class="overlay"></div> </div> <div id="two" class="box"></div> </div> </div> 

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

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