鼠标移动时滚动窗口

kev*_*vin 6 javascript mouse scroll acceleration

大家好

我的意思是当鼠标移向窗口边缘(x或y或两者)时,我希望页面滚动,当鼠标停止移动时,我希望页面停止滚动.

有很多关于如何基于在窗口边缘使用onClick事件或滚动区域进行滚动的示例,但基于鼠标光标的移动并不多.

任何帮助将非常感激.

bry*_*mck 5

网页已经设计为使用滚动条,页面/主页/结束/箭头键等滚动.是否有任何理由不适合您的页面?改变预期的功能通常不是一个好主意.

你可以在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;
Run Code Online (Sandbox Code Playgroud)