Scroll the window while moving the mouse

Hello everyone.

What do I mean when the mouse moves to the edge of the window (x or y or both), I want the page to scroll, and when the mouse stops moving, I want the page to stop scrolling.

There are many examples of scrolling based on the use of the onClick event or the scroll zone at the edge of the window, but not so much based on the movement of the mouse cursor.

Any help would be greatly appreciated.

+5
source share
1 answer

- , /// .. - , ? .

mousemove. , , . :

// Variables for current position
var x, y;

function handleMouse(e) {
  // Verify that x and y already have some value
  if (x && y) {
    // Scroll window by difference between current and previous positions
    window.scrollBy(e.clientX - x, e.clientY - y);
  }

  // Store current position
  x = e.clientX;
  y = e.clientY;
}

// Assign handleMouse to mouse movement events
document.onmousemove = handleMouse;
+5

All Articles