繁体   English   中英

在以下情况下,使用Javascript或jquery滚动到页面底部

[英]Scroll to bottom of page using Javascript or jquery with these conditions

我试图刺激一个网页聊天室,其页面需要为:

  1. 如果用户未滚动,则经过一定时间间隔后滚动到页面底部。
  2. 如果用户已滚动,请停止滚动。
  3. 如果用户到达div #end则开始滚动到页面底部
<script>
    var scroll = 1;
    function autoScrolling(scroll) {
    if(scroll==1){
    window.scrollTo(0,document.body.scrollHeight);
    }
    }
    window.onscroll = function() {
    setInterval(autoScrolling(0), 1000);
    }
    window.addEventListener("scroll", function() {
      var elementTarget = document.getElementById("end");
      if (window.scrollY > (elementTarget.offsetTop + elementTarget.offsetHeight)) {
          scroll=1;
         setInterval(autoScrolling(0), 1000);
      }
    });
    </script>

我正在应用此逻辑,但是它不起作用。

尝试这个,

    var scrolled = false;

 $(document).ready(function(){

       //First requirement
       window.setInterval(function(){
          checkScroll();
        }, 5000);

         //Second requirement
        window.onscroll = function() { setScroll()};

         //Third requirement
        $('#end').on('scroll', function() {
            if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
                autoScroll();
            }
        })
})

function autoScroll(){
    window.scrollTo(0,document.body.scrollHeight);
}

function setScroll(){
      if (window.scrollY > (elementTarget.offsetTop + elementTarget.offsetHeight)) {
          scroll=true;
      }
    }
}

function checkScroll(){
    if(!scrolled) 
        autoScroll();
    scrolled = false;
}

暂无
暂无

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

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