Home > Software engineering >  I want the page to scroll down, top automatically when my cursor is near the top or bottom of the pa
I want the page to scroll down, top automatically when my cursor is near the top or bottom of the pa

Time:02-16

For example, I want the page to scroll down automatically when I approach until there is 10px of space at the bottom. I want the same scenario to happen when I move the cursor up.

#list-container{
    background-color: rebeccapurple;
    cursor: default;
    overflow-y: auto;
    height: 200px;
    padding: 10px;
}
#list{
    padding: 10px;
    margin: 10px;
}
        <div id="list-container">
            <ol id="list" >
                <li>zero</li>
                <li>one</li>
                <li>two</li>
                <li>three</li>
                <li>four</li>
                <li>five</li>
                <li>six</li>
                <li>seven</li>
                <li>eight</li>
                <li>nine</li>
                <li>ten</li>
                <li>eleven</li>
                <li>twelve</li>
                <li>thirteen</li>
                <li>fourteen</li>
                <li>fifteen</li>
            </ol>
        </div>

CodePudding user response:

Check the following code, it might help:

let false = false;

window.addEventListener('scroll', () => {
    if(window.innerHeight   window.scrollY >= document.body.offsetHeight - 1000 && ready)
    {
        ready = false;
        << your code here >>
    }
});

CodePudding user response:

I was able to solve my problem like this guys, I hope I can help someone else

 $("body").bind("mouseover", function (e) {
        e = e || window.event; var cursor = { y: 0 }; cursor.y = e.pageY; //Cursor YPos
        var papaWindow = parent.window;
        var $pxFromTop = $(papaWindow).scrollTop();
        var $userScreenHeight = $(papaWindow).height();

        if (cursor.y > (($userScreenHeight   $pxFromTop) / 1.15)) {

          if ($pxFromTop < ($userScreenHeight * 3.2)) {

            papaWindow.scrollBy(0, ($userScreenHeight / 30));
          }
        }
        else if (cursor.y < (($userScreenHeight   $pxFromTop) * 0.75)) {

          papaWindow.scrollBy(0, -($userScreenHeight / 30));

        }
      });

and where your code ends after binding to "body"

$("body").unbind("mouseover");

if we don't want it to follow our mouse we have to unbind it

  • Related