如何使用scrollIntoView在div中滚动而不滚动整个页面?

时间:2019-10-09 20:12:36

标签: javascript html css angular scroll

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中滚动页面?

0 个答案:

没有答案
相关问题