你不能做你想做的事情,如果你直接嵌套了固定的parent-child
,它将不会工作
html
的 struct 必须如下所示
<div class="wrapper"> <<<<< this is fixed
<div class="internal-scroll"> <<<<< this is relative
<div class="overlay"> <<<<<< this is fixed
<a>hover me, where the scrollbar is</a>
</div>
</div>
</div>
.wrapper {
position: fixed;
// add top, right, bottom, left
}
.internal-scroll {
top: 5px;
left: 5px;
width: 200px;
height: 200px;
overflow-y: auto;
border: LightCoral 2px solid;
}
.overlay {
position: fixed;
top: 40px;
left: 160px;
width: 100px;
padding: 5px;
background: aliceblue;
border: solid 2px SlateBlue;
z-index: 999;
}
.overlay a:hover {
background: green;
cursor: pointer;
}
<div class="wrapper">
<div class="internal-scroll">
<div class="overlay">
<a>hover me, where the scrollbar is</a>
</div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
</div>
</div>
另外,请记住,固定元素不同于绝对元素,不要以任何上升relative
的父元素作为定位参考,它们以视区为参考.所以实际上没有必要有fixed
,parent-child
个元素.
你可以把overlay
放在internal-scroll
之外,然后就会有同样的行为.
请参见示例:
.internal-scroll {
position: fixed;
top: 5px;
left: 5px;
width: 200px;
height: 200px;
overflow-y: auto;
border: LightCoral 2px solid;
}
.overlay {
position: fixed;
top: 40px;
left: 160px;
width: 100px;
padding: 5px;
background: aliceblue;
border: solid 2px SlateBlue;
z-index: 999;
}
.overlay a:hover {
background: green;
cursor: pointer;
}
<div class="internal-scroll">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
</div>
<div class="overlay">
<a>hover me, where the scrollbar is</a>
</div>