簡體   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