[javascript] detect scroll end

Posted in :

常看到網頁要求使用者捲到最下,看完長長的宣告文章,才可以繼續,javascript 範例:
https://stackoverflow.com/questions/3962558/javascript-detect-scroll-end

function scrolled(e) {
  if (myDiv.offsetHeight + myDiv.scrollTop >= myDiv.scrollHeight) {
    scrolledToBottom(e);
  }
}

or

function GetScrollerEndPoint()
{
   var scrollHeight = $("#myDiv").prop('scrollHeight');
   var divHeight = $("#myDiv").height();
   var scrollerEndPoint = scrollHeight - divHeight;

   var divScrollerTop =  $("#myDiv").scrollTop();
   if(divScrollerTop === scrollerEndPoint)
   {
       //Your Code 
       //The Div scroller has reached the bottom
   }
}

or

    $('.scroll').scroll(function() {
        if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
            alert('Bottom reached!');
        }
    });

相反的,要與之對抗用的 selenium:
How to scroll down the page till page end in the Selenium WebDriver?

execute_script("arguments[0].scrollTo(0, arguments[0].scrollHeight)", scrollArea);

or

execute_script("arguments[0].scrollIntoView(true);", scrollArea);

但,上面的解法,真的很奇怪,我使用 execute_script 把 variable 內容輸出到 browser console.log() 但,數值都是正確的,但沒有 scroll to end.

我用的解法是:

driver.execute_script("arguments[0].innerHTML='';", scrollArea);

直接把 div innerHTML 清空,就完成 scroll to end event.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *