javascript - 如何使用js,jquery移动表格滚动条?

标签 javascript jquery html css

我有一个带有水平滚动条的表格,如下所示。

 <table >
    <tr>
        <th>Col1</th>
        <th>col2</th>
        <th>col3</th>
        <th>col4</th>
        <th>col5</th>
        <th>col5</th>
    </tr>
</table>

enter image description here 有没有办法使用 javascript(dynamicaly) 移动滚动条。例如,我想在单击按钮时将滚动条向右移动 30px。

最佳答案

点击右侧向右滚动,点击左侧向后滚动。这就是它的工作原理。干杯。

$(".leftArrow").click(function () { 
  var leftPos = $('.wrapper').scrollLeft();
  $(".wrapper").animate({scrollLeft: leftPos - 30}, 100);
});

$(".rightArrow").click(function () { 
  var leftPos = $('.wrapper').scrollLeft();
  $(".wrapper").animate({scrollLeft: leftPos + 30}, 100);
});
.wrapper {
  width:100px;
  overflow-x: scroll;
  overflow-y: hidden;
}

table {
  width: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <table>
    <tr>
      <th>Col1</th>
      <th>col2</th>
      <th>col3</th>
      <th>col4</th>
      <th>col5</th>
      <th>col5</th>
    </tr>
  </table>
</div>

<button class="leftArrow">Scroll left</button>

<button class="rightArrow">Scroll right</button>

关于javascript - 如何使用js,jquery移动表格滚动条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37939521/

相关文章:

javascript - Neo4j 和 Neovis.js

javascript - 避免使用 dygraphs javascript 在 y 轴上 float 数字标签,并将不同的网格线设置为辅助 y 轴

c# - 使用 JS 调用 C# 方法

javascript - 表单提交后重置文本字段

javascript - 这个 comment/*@cc_on!@*/0 在 javascript 的 if 语句中做什么?

javascript - 如何定位下一个 <div> 元素?

javascript - 如何在select2上触发事件

javascript - 如何使选择下拉菜单响应输入框

html - 令人困惑的 HTML/CSS 图标显示在我所有的 iOS 设备上,但其他设备上没有

javascript - jQuery - 根据子容器的内容向父容器添加类