简体   繁体   English

Javascript - 我可以让这个表格滚动到顶部吗?

[英]Javascript - Can I make this table scroll to top?

I have to make a self-scrolling table.我必须制作一个自滚动表。 I tried using this: https://codepen.io/salman31/pen/dYdGLa我试过用这个: https://codepen.io/salman31/pen/dYdGLa

    var my_time;
$(document).ready(function() {
  pageScroll();
  $("#contain").mouseover(function() {
    clearTimeout(my_time);
  }).mouseout(function() {
    pageScroll();
  });
});

function pageScroll() {  
    var objDiv = document.getElementById("contain");
  objDiv.scrollTop = objDiv.scrollTop + 1;  
  $('p:nth-of-type(1)').html('scrollTop : '+ objDiv.scrollTop);
  $('p:nth-of-type(2)').html('scrollHeight : ' + objDiv.scrollHeight);
  //if (objDiv.scrollTop == (objDiv.scrollHeight - 50)) {
    objDiv.scrollTop = 0;
  //}
  my_time = setTimeout('pageScroll()', 25);
}

And it works, but it doesn't automatically scroll back to top when it hits the "bottom".它可以工作,但是当它到达“底部”时它不会自动滚动回顶部。 What do I need to change?我需要改变什么?

Thank you!谢谢!

try this尝试这个

var my_time;
$(document).ready(function() {
  pageScroll();
  $("#contain").mouseover(function() {
    clearTimeout(my_time);
  }).mouseout(function() {
    pageScroll();
  });

  $('#contain').bind('scroll', function()
     {
       if($(this).scrollTop() + $(this).innerHeight()>=$(this)[0].scrollHeight)
          {
              pageScroll();
          }
     })
});

function pageScroll() {  
    var objDiv = document.getElementById("contain");
  objDiv.scrollTop = objDiv.scrollTop + 1;  
  $('p:nth-of-type(1)').html('scrollTop : '+ objDiv.scrollTop);
  $('p:nth-of-type(2)').html('scrollHeight : ' + objDiv.scrollHeight);
  if (objDiv.scrollTop == (objDiv.scrollHeight - 100)) {
    objDiv.scrollTop = 0;
  }
  my_time = setTimeout('pageScroll()', 25);
}

The line:该行:

objDiv.scrollTop = 0

is what's supposed to reset the scroll position, using the if statement you've got commented to execute it only once it's finished scrolling up.是什么应该重置滚动 position,使用您已注释的 if 语句仅在完成向上滚动后执行它。 With the if statement commented out, the table shouldn't be scrolling at all, as the pageScroll function will always set the scrollTop property to 0.注释掉 if 语句后,表格根本不应该滚动,因为 pageScroll function 将始终将 scrollTop 属性设置为 0。

Try uncommenting the if statement, and making sure that you're subtracting the height of your container from objDiv.scrollHeight in the comparison.尝试取消注释 if 语句,并确保在比较中从 objDiv.scrollHeight 中减去容器的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在点击时让 div 滚动到页面顶部? - How can I make a div scroll to the top of the page on click? 如何将列表框中的选定项目滚动到顶部? - How can I make selected item in listbox scroll to the top? 如何使用 Javascript 滚动到页面顶部? - How can I scroll to the top of a page using Javascript? Javascript:让网页浏览器滚动到顶部? - Javascript: make the web browser scroll to the top? 如何使图像出现在 javascript 中(并在放大时滚动) - How can I make a image appear in javascript (and scroll whilst zoomed in) 如何让javascript自动滚动,在HTML做一个滚动条? - How can I make javascript automatically scroll and make a scroll bar in HTML? 当我第一次滚动到菜单时,如何使菜单“粘”到顶部? - How can I make a menu "sticky" to the top when I scroll to it for the first time? 如何在滚动固定标题/导航栏上制作多个贴在页面顶部的固定标题/导航栏? - How can I make multiple on scroll fixed headers/navbars that stick at the very top of the page? 导航栏展开时,如何使它滚动并固定到页面顶部? - How can I make the navbar scroll and fix to the top of the page when it is expanded? 如何使导航栏平滑滚动到页面顶部,请单击 - how can I make my navbar scroll smoothly to the top on page click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM