我刚看到了 Vlad Danila 的一个答案,但是无法重现平滑滚动效果。有人能帮我吗?
以下是我整理的一个示例代码片段:
const buttonRight = document.getElementById('slideRight');
const buttonLeft = document.getElementById('slideLeft');
buttonRight.onclick = function() {
document.getElementById('container').scrollLeft += 20;
};
buttonLeft.onclick = function() {
document.getElementById('container').scrollLeft -= 20;
};
#container {
width: 145px;
height: 100px;
border: 1px solid #ccc;
overflow-x: scroll;
}
#content {
width: 250px;
background-color: #ccc;
}
<div id="container">
<div id="content">Click the buttons to slide horizontally!</div>
</div>
<button id="slideLeft" type="button">Slide left</button>
<button id="slideRight" type="button">Slide right</button>