Html 如何制作<;部门>;可从底部滚动

Html 如何制作<;部门>;可从底部滚动,html,css,Html,Css,我在一个div里做了一个表格,使它可以滚动 为此,您需要使用javascript。使用scrollTop访问从顶部开始的滚动偏移(默认值为0)。因此,您必须获取div的高度,并将div的scrollTop设置为其高度。为了获得高度,我使用了getBoundingClientRect().height let tableDiv=document.getElementById('main'); tableDiv.scrollTop=tableDiv.getBoundingClientRect(

我在一个div里做了一个表格,使它可以滚动



为此,您需要使用javascript。使用
scrollTop
访问从顶部开始的滚动偏移(默认值为0)。因此,您必须获取div的高度,并将div的
scrollTop
设置为其高度。为了获得高度,我使用了
getBoundingClientRect().height

let tableDiv=document.getElementById('main');
tableDiv.scrollTop=tableDiv.getBoundingClientRect().height

您的HTML内容:

<div class="scorllable-table" id="scrollTable">
  <table>
    <thead>
      <tr>
        <th>a</th>
        <th>b</th>
      </tr>
    </thead>
    
    <tbody>
      <tr>
        <td>a</td>
        <td>a</td>
      </tr>
      <tr>
        <td>b</td>
        <td>b</td>
      </tr>
      <tr>
        <td>t</td>
        <td>t</td>
      </tr>
      <tr>
        <td>a</td>
        <td>a</td>
      </tr>
      <tr>
        <td>b</td>
        <td>b</td>
      </tr>
      <tr>
        <td>t</td>
        <td>t</td>
      </tr>
    </tbody>
  </table>
</div>
如果您只想使用JavaScript来实现这一点,那么请尝试使用它

var scrollableDiv = document.getElementById("scrollTable");
scrollableDiv.scrollTop = scrollableDiv.scrollHeight;
$('#scrollTable').stop().animate({
  scrollTop: $('#scrollTable')[0].scrollHeight
}, 500);
如果要使用jQuery执行此操作,请使用以下代码

$("#scrollTable").scrollTop($("#scrollTable")[0].scrollHeight);
如果要平滑滚动,请尝试使用此选项

var scrollableDiv = document.getElementById("scrollTable");
scrollableDiv.scrollTop = scrollableDiv.scrollHeight;
$('#scrollTable').stop().animate({
  scrollTop: $('#scrollTable')[0].scrollHeight
}, 500);
祝你愉快,谢谢。:)