为了账号安全,请及时绑定邮箱和手机立即绑定

如何为水平滚动添加平滑滚动效果?

如何为水平滚动添加平滑滚动效果?

30秒到达战场 2024-01-18 15:45:13
无法重现平滑滚动效果。谁能帮我?这是我整理的示例代码片段: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>
查看完整描述

1 回答

?
喵喔喔

TA贡献1735条经验 获得超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>



查看完整回答
反对 回复 2024-01-18
  • 1 回答
  • 0 关注
  • 25 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信