1 回答

TA貢獻1735條經(jīng)驗 獲得超5個贊
您可以使用原生 CSS 屬性scroll-behavior: smooth
,如下所示:
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;
? scroll-behavior: smooth;
}
#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>
添加回答
舉報