scroll window when mouse moves

后端 未结 1 1538
刺人心
刺人心 2021-02-09 23:14

Hello all

What I mean is while the mouse is moving towards the edge of the window (x or y or both), I want the page to scroll, and when the mouse stops moving, I want t

1条回答
  •  灰色年华
    2021-02-09 23:45

    Web pages are already designed to scroll using the scroll bar, page/home/end/arrow keys, etc. Is there any reason that's insufficient for your page? It's usually not a good idea to alter expected functionality.

    You can read up on the mousemove event here. Anyway, the code below should work, but I really don't recommend using it. It can be especially disorienting for people with sensitive mice:

    // 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;
    

    0 讨论(0)
提交回复
热议问题