【发布时间】:2021-03-11 16:22:47
【问题描述】:
我的页面上有两个溢出的元素,我想同时调用ScrollIntoView 来获取它们中的子元素。
以下内容适用于 Firefox,但不适用于 Chrome。这是一个错误吗?
const button = document.querySelector('button');
const one = document.querySelector('.one');
const two = document.querySelector('.two');
button.addEventListener('click', () => {
one.scrollIntoView({ block: 'center', behavior: 'smooth' });
two.scrollIntoView({ block: 'center', behavior: 'smooth' });
});
body {
text-align: center;
}
#container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
width: 100%;
height: 130px;
}
#container > div {
max-height: 200px;
overflow-y: auto;
border: 1px solid black;
}
button {
font-size: 1.5rem;
margin: 0.5rem auto;
}
<div id="container">
<div>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
<h1 class="one">Boo!</h1>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
</div>
<div>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
<h1 class="two">Boo!</h1>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
<h1>...</h1>
</div>
</div>
<button>click to scroll</button>
【问题讨论】:
-
我用
scrollTo()给了你解决方案。您有任何问题或要求吗?我的代码中有什么需要做的吗?并尽量避免使用 ScrollIntoView。因为有缺点。例如 - stackoverflow.com/questions/65953805/…
标签: javascript html google-chrome