skip to Main Content
let prevDom = null;

document.addEventListener("click", function(e) {
  e.preventDefault();
}, false);

document.body.style.cursor = "default !important";

document.addEventListener("mousemove", function(e) {
  e.target.style.pointerEvents = "none";
  if (prevDom !== null) {
    prevDom.style.pointerEvents = "";
  }
  prevDom = e.target;
}, false);
<a href="/">click me!!!!</a>
<br/>
<br/>
<a href="/">no click me!!!!</a>
<br/>
<br/>
<a href="/">hahaha</a>

I’m making an extension that allows the user to highlight DOM elements by hovering on them. When the user is hovering, I want to disable click events and keep the cursor in its default state.
For that, I’ve added in the following code:

document.addEventListener("click", function(e) {
    e.preventDefault();
}, false);

document.body.style.cursor = "default !important";

While this snippet prevents any click events from firing, it doesn’t prevent the cursor from changing to pointer when hovering on anchor tags. I tried to simply disable pointer events when hovering on an element via an event listener for mousemove, but the issue here is that it flickers between the two states. Like, you see the pointer for a fraction of a second, before it switches to default. This really hampers the UX, so I was wondering if there’s a way to prevent the cursor from changing to pointer.

Click here to see an example

3

Answers


  1. In your CSS:

    a:hover {
      cursor: default;
      pointer-events: none;
    }
    
    Login or Signup to reply.
  2. Use cursor: default !important;, but for the a:hover state as shown below.

    If you need it to apply only to certain links on the page, use a class on the link and a.yourclass:hover in the CSS rule.

    a.myclass:hover {
      cursor: default !important;
    }
    <a href="#" class="myclass">click me!!!!</a>
    <br/>
    <br/>
    <a href="#" class="myclass">no click me!!!!</a>
    <br/>
    <br/>
    <a href="#" class="myclass">hahaha</a>
    Login or Signup to reply.
  3. The simplest approach would be to add an css rule for all anchor tags like this

    a {
    
    cursor: default;
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search