使用CSS属性ovescroll-bahavior
,我们可以在内部元素到达顶部或底部时防止滚动父元素。但是当子元素中的内容太少而无法滚动时,父元素将被滚动。为了保持一致性,我希望在这种情况下不发生滚动。我该如何实现?
.child {
height: 5em;
width: 5em;
margin: 1em;
background-color: lightgrey;
overflow-y: scroll;
overscroll-behavior-y: contain;
}
.parent {
height: 8em;
overflow-y: scroll;
border: solid;
}
<div class="parent">
If you try to scroll the first box, the outer box should never be scrolled. But if you try to scroll the second box, the outer box is scrolled, which is undesired.
<div class="child">
1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10
</div>
<div class="child">
1<br>2<br>3
</div>
</div>
overscroll-behavior-y
在child
中按预期工作,child
需要是可滚动的。否则,您将需要使用 JS。 - Kameron