我嘗試自動滾動這個 div 但我被卡住了,因為我不能做我想做的事。這個片段是從 Stackoverflow 中檢索到的,它通常應該水平移動。但是我的目標是垂直制作。
我該怎么做?
const flavoursContainer = document.getElementById('flavoursContainer');
const flavoursScrollHeight = flavoursContainer.scrollHeight;
window.addEventListener('load', () => {
self.setInterval(() => {
if (flavoursContainer.scrollTop !== flavoursScrollHeight) {
flavoursContainer.scrollTo(flavoursContainer.scrollTop 1, 0);
}
}, 15);
});
.container {
height: 100px;
overflow-x: scroll;
white-space: nowrap;
background-color: #fff;
}
<div class="container" id="flavoursContainer">
<div class="element">test</div>
<div class="element">test</div>
<div class="element">test</div>
<div class="element">test</div>
<div class="element">test</div>
<div class="element">test</div>
<div class="element">test</div>
<div class="element">test</div>
<div class="element">test</div>
</div>
uj5u.com熱心網友回復:
您需要使用該scrollTo(x, y)方法的第二個引數。
例如:
flavoursContainer.scrollTo(0, flavoursContainer.scrollTop 1);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/377299.html
標籤:javascript html 查询 css 滚动
