有demo
代码是:
scrollToElement() {
document.getElementById("elem-17").scrollIntoView();
}
html是:
<button (click)="scrollToElement()">Scroll To 8th Element</button>
<div class="wrapper">
<li *ngFor="let item of itemList; let i = index" id="elem-{{i}}">{{item}}</li>
</div>
css是:
.wrapper {
height: 200vh;
overflow-y: scroll;
}
scrollIntoView方法滚动“包装器” div,并且当div的高度不高时。但是,当div高度大于页面视口时,它会滚动整个页面。如何在div中滚动页面?