我正试图将div留在另一个div内的固定位置。包含的div是可滚动的,并且他的位置不会在屏幕中固定。
这是我到目前为止所获得的JSFiddle
滚动时,文本“固定文本”应保留在容器的右上角。 为了模拟div的2个不同位置,我制作了2个div并保留了同一个类。
只能用CSS完成吗?
HTML
<div class="cont">
<div class="items">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="txt">
fixed text
</div>
</div>
<div class="cont">
<div class="items">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="txt">
fixed text
</div>
</div>
CSS
.cont{
width: 400px;
height: 130px;
border: 1px solid black;
margin: 10px;
overflow-x: auto;
overflow-y: hidden;
position: relative;
}
.items{
width: 600px;
}
.item{
width: 80px;
height: 80px;
background-color: blue;
margin-top: 22px;
margin-left: 3px;
display: inline-block;
}
.txt{
position: absolute;
top: 2px;
right: 10px;
}
答案 0 :(得分:2)
您可以overflow: auto
使用items
代替cont
元素。
.cont {
width: 400px;
height: 130px;
border: 1px solid black;
margin: 10px;
overflow-x: hidden;
position: relative;
}
.items {
overflow-x: auto;
overflow-y: hidden;
white-space: nowrap;
}
.item {
width: 80px;
height: 80px;
background-color: blue;
margin-top: 22px;
margin-left: 3px;
display: inline-block;
}
.txt {
position: absolute;
top: 2px;
right: 10px;
}
&#13;
<div class="cont">
<div class="items">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="txt">
fixed text
</div>
</div>
&#13;
答案 1 :(得分:1)
创建额外的包装并将height
和overflow
应用于此 - 我添加了一个div inner
,其中包含cont
的所有内容并将其应用于此:
.inner {
overflow-x: scroll;
overflow-y: hidden;
height: inherit;
}
请参阅下面的演示了解我的意思:
.cont {
width: 400px;
height: 130px;
border: 1px solid black;
margin: 10px;
/*overflow-x: auto;*/
/*overflow-y: hidden;*/
position: relative;
}
.items {
width: 600px;
}
.item {
width: 80px;
height: 80px;
background-color: blue;
margin-top: 22px;
margin-left: 3px;
display: inline-block;
}
.txt {
position: absolute;
top: 2px;
right: 10px;
}
.inner {
overflow-x: scroll;
overflow-y: hidden;
height: inherit;
}
<div class="cont">
<div class="inner">
<div class="items">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="txt">
fixed text
</div>
</div>
</div>
答案 2 :(得分:0)
将固定高度和溢出添加到.items
,以便.items
滚动而不是.cont
,.txt
将保持原样,因为它相对于.cont
定位.cont
和.cont{
width: 400px;
border: 1px solid black;
margin: 10px;
overflow-x: auto;
overflow-y: hidden;
position: relative;
}
.items{
width: 600px;
height: 130px;
overflow: scroll;
}
.item{
width: 80px;
height: 80px;
background-color: blue;
margin-top: 22px;
margin-left: 3px;
display: inline-block;
}
.txt{
position: absolute;
top: 2px;
right: 10px;
}
不会滚动。
<div class="cont">
<div class="items">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="txt">
fixed text
</div>
</div>
<div class="cont">
<div class="items">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="txt">
fixed text
</div>
</div>
{{1}}