小编Ahm*_*uda的帖子

如何使用按钮在 div 中水平滚动动画

我发现这段代码添加了在 div 中水平滚动的按钮,它是由 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;
};
Run Code Online (Sandbox Code Playgroud)
#container {
  width: 145px;
  height: 100px;
  border: 1px solid #ccc;
  overflow-x: scroll;
}

#content {
  width: 250px;
  background-color: #ccc;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

html javascript css

3
推荐指数
1
解决办法
4502
查看次数

标签 统计

css ×1

html ×1

javascript ×1