简体   繁体   English

如何不断检查是否有div溢出然后滚动到底部

[英]How to constantly check whether there is div overflow and then scroll to the bottom

I'm creating a div which on overflow goes to the bottom straight away.But how do I constantly check whether an overflow has occurred or not and then fire the function which will scroll down to the bottom of the page? 我正在创建一个div,一旦溢出,该div会立即到达底部,但是我如何不断检查是否发生了溢出,然后触发将滚动到页面底部的函数呢?

I found this function which scrolls to the bottom 我发现此功能滚动到底部

 <script type="text/javascript">
  var objDiv = document.getElementsByClassName('container')[0];
   objDiv.scrollTop = objDiv.scrollHeight;
 <script>

and this function which checks whether there is an overflow or not 这个函数检查是否有溢出

 <script type="text/javascript">
  function isOverflown(element) {
    return element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth;
     }
  </script>

But how do I constantly listen when the overflow occurs? 但是,发生溢出时,我该如何不断聆听呢? Also, I have one more problem after always scrolling to the bottom of the page, if I manually want to go on the top, it automatically scrolls to the bottom.How do I prevent this? 此外,总是滚动到页面底部后,我还有另一个问题,如果我手动想移到页面顶部,它将自动滚动到页面底部,如何防止这种情况发生?

For run code continues, you can use setInterval function. 若要继续运行代码,可以使用setInterval函数。 It takes two params, the callback , and the time in milliseconds . 它需要两个参数, 回调 ,并以毫秒为单位时间 It runs the code all the time, so you can put there function to be executed. 它一直在运行代码,因此您可以在其中放置要执行的功能。

If the content is dynamic you can use also callback to check that after render, it should be recalculated. 如果内容是动态的,则还可以使用回调检查渲染后是否应重新计算。

But, why do you want to do this? 但是,为什么要这样做呢?

Thanks to @Piotr P. I found the way to do this, but now i'm unable to scroll it manually to other parts of the screen 感谢@PiotrP。我找到了执行此操作的方法,但是现在我无法手动将其滚动到屏幕的其他部分

  setInterval(function(){
      var element = document.getElementsByClassName('container')[0];
      if (element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth) {
        $('#mydiv').animate({
                   scrollTop: $(document).height()
               },
                'slow');
               return false;
      }
    }, 100);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM