当用户的鼠标到达页面的顶部/底部时滚动页面

时间:2020-01-21 06:13:21

标签: javascript scroll drag-and-drop

我正在尝试创建拖放,并且希望滚动窗口,以防我开始拖动并到达页面的顶部/底部,直到走出顶部/底部“区域”为止。到目前为止,我所写的作品以不同的方式工作,我无法找到一种使之按我想要的方式工作的方法。 使用香草JS有什么方法吗?

let mouseDown = true;

function mousedown() {
  mouseDown = true;
}

function mouseup() {
  mouseDown = false;
}
if (!document.querySelector(".arrow-timeline")) {
  this.element.addEventListener('mousemove', function() {
    let x, y;

    function handleMouse(e) {
      // Verify that x and y already have some value
      if (x && y && mouseDown) {
        // 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.onmousedown = mousedown;
    document.onmousemove = handleMouse;
    document.onmouseup = mouseup;
  })
}

0 个答案:

没有答案
相关问题