0

I need to hide the cursor when dragging on the web browser. It's doesn't have to be an HTML element. If I click anywhere on the page and click and drag, the cursor should be hidden. When dragging finished cursor should be shown again. The cursor can't be hide when I click on the other buttons on the page. It should only be hidden when click and drag not when clicked. Is there any way I can achieve this using CSS and JavaScript? Thanks for your help.

5
  • see: drag, dragend, and cursor Commented May 23, 2021 at 15:20
  • you might want to take a glace at this: stackoverflow.com/q/31829975/15777388 Commented May 23, 2021 at 15:38
  • i dont know if it will answer your question, buts its worth a shot. Commented May 23, 2021 at 15:38
  • @JSman225 I have seen it before, but it's exactly not what I need. It also included jQuery Commented May 23, 2021 at 15:42
  • You can hide the cursor by css cursor: none; Commented May 23, 2021 at 16:31

1 Answer 1

1

The simplest solution is just use mousemove event

var dragElement = null;
document.addEventListener("mousemove", e =>
{
  const isDragging = e.buttons == 1 && (e.movementX || e.movementY);
  document.body.classList.toggle("drag", isDragging);
  if (isDragging)
  {
    if (!dragElement)
      dragElement = e.target;

    console.log("dragging", dragElement);
  }
  else
  {
    dragElement = null;
  }
});
html, body
{
  height: 100%;
  width: 100%;
}
body.drag
{
  cursor: none;
  user-select: none;
}

#test
{
  width: 10em;
  height: 10em;
  background-color: pink;
}
<div id="test">test</div>

However this method will trigger "dragging" even if user clicked outside of the page. To solve this, we can track mousedown and mouseup events:

var dragElement = null;
document.addEventListener("mousedown", e =>
{
  dragElement = e.target;
});

document.addEventListener("mouseup", e =>
{
  dragElement = null;
});

document.addEventListener("mousemove", e =>
{
  const isDragging = dragElement && (e.movementX || e.movementY);
  document.body.classList.toggle("drag", isDragging);
  if (isDragging)
  {
    console.log("dragging", dragElement);
  }
});

document.body.appendChild(document.createElement("canvas"));
html, body
{
  height: 100%;
  width: 100%;
}
body.drag
{
  cursor: none;
  user-select: none;
}

#test
{
  width: 30vw;
  height: 30vh;
  background-color: pink;
}

canvas
{
  width: 30vw;
  height: 30vh;
  background-color: lightgreen;
}
<div id="test">test</div>

Sign up to request clarification or add additional context in comments.

3 Comments

It worked perfectly on the body of the page, but there is a little problem, I'm working with three js library and the canvas is autogenerated by the javascript, so it doesn't work when I click and drag on that canvas. Can you tell me how to do this for a canvas as well? I tried but not working
It works fine with canvas element that was added via javascript, as you can see in second example. So there must be something else in your code that changes cursor.
doesnt work with elements that have the draggable attribute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.