`Cannot press when using custom cursor in JS?`

Hey there! I'm currently working on implementing a custom cursor for specific elements on my website. However, I've encountered a little hiccup - once the custom cursor is in place, I'm no longer able to click on any elements. Does anyone have any insight into what might be causing this issue?

Check out my code here

$('a').on('click',function(){
  alert('sad');
});

(function() {
  var follower, init, mouseX, mouseY, positionElement, printout, timer;

  follower = document.getElementById('follower');

  printout = document.getElementById('printout');

  mouseX = (function(_this) {
    return function(event) {
      return event.clientX;
    };
  })(this);

  mouseY = (function(_this) {
    return function(event) {
      return event.clientY;
    };
  })(this);

  positionElement = (function(_this) {
    return function(event) {
      var mouse;
      mouse = {
        x: mouseX(event),
        y: mouseY(event)
      };
      follower.style.top = mouse.y + 'px';
      return follower.style.left = mouse.x + 'px';
    };
  })(this);

  timer = false;

  window.onmousemove = init = (function(_this) {
    return function(event) {
      var _event;
      _event = event;
      return timer = setTimeout(function() {
        return positionElement(_event);
      }, 1);
    };
  })(this);

}).call(this);

Answer №1

Your unique cursor design may be blocking mouse clicks on the webpage below.

Instead of resorting to complex z-index adjustments or toggling the visibility of the cursor, you can utilize pointer-events property:

#custom-cursor {
    pointer-events: none;
}

By doing this, any mouse or touch events will pass through your custom cursor to interact with the elements underneath. Keep in mind that even hover effects like :hover will still work, meaning the default cursor may appear over links unless you explicitly hide it, as shown below:

a:hover {
    cursor: none;
} 

Answer №2

To ensure that the click event only affects the #follower element, add z-index: -1 to the CSS for #follower. This will establish a stacking order where the #follower element takes precedence over other elements that are statically positioned.

$('a').on('click',function(){
  alert('sad');
});

(function() {
  var follower, init, mouseX, mouseY, positionElement, printout, timer;

  follower = document.getElementById('follower');

  printout = document.getElementById('printout');

  mouseX = (function(_this) {
    return function(event) {
      return event.clientX;
    };
  })(this);

  mouseY = (function(_this) {
    return function(event) {
      return event.clientY;
    };
  })(this);

  positionElement = (function(_this) {
    return function(event) {
      var mouse;
      mouse = {
        x: mouseX(event),
        y: mouseY(event)
      };
      follower.style.top = mouse.y + 'px';
      return follower.style.left = mouse.x + 'px';
    };
  })(this);

  timer = false;

  window.onmousemove = init = (function(_this) {
    return function(event) {
      var _event;
      _event = event;
      return timer = setTimeout(function() {
        return positionElement(_event);
      }, 1);
    };
  })(this);

}).call(this);
html {
  cursor: none;
  background: #666;
  height: 5000px;
}
#follower {
  position: fixed;
  top: 50%;
  left: 50%;
  z-index: -1;
}
#follower #circle1 {
  position: absolute;
  -webkit-animation: pulse 2s infinite; /* Chrome, Safari, Opera */
  animation: pulse 2s infinite;
  background: #fff;
  border-radius: 50%;
  height: 0em;
  width: 0em;
  margin-top: 0em;
  margin-left: 0em;
}
#follower #circle2 {
  position: absolute;
  -webkit-animation: pulse 4s infinite; /* Chrome, Safari, Opera */
  animation: pulse 4s infinite;
  background: rgba(200,0,0,0.8);
  border-radius: 50%;
  height: 0em;
  width: 0em;
  margin-top: 0em;
  margin-left: 0em;
}
@-moz-keyframes pulse {
  0% {
    opacity: 0.2;
    height: 1em;
    width: 1em;
    margin-top: -0.5em;
    margin-left: -0.5em;
  }
  50% {
    opacity: 0.9;
    height: 3em;
    width: 3em;
    margin-top: -1.5em;
    margin-left: -1.5em;
  }
  100% {
    opacity: 0.2;
    height: 1em;
    width: 1em;
    margin-top: -0.5em;
    margin-left: -0.5em;
  }
}
@-webkit-keyframes pulse {
  0% {
    opacity: 0.2;
    height: 1em;
    width: 1em;
    margin-top: -0.5em;
    margin-left: -0.5em;
  }
  50% {
    opacity: 0.9;
    height: 3em;
    width: 3em;
    margin-top: -1.5em;
    margin-left: -1.5em;
  }
  100% {
    opacity: 0.2;
    height: 1em;
    width: 1em;
    margin-top: -0.5em;
    margin-left: -0.5em;
  }
}
@-o-keyframes pulse {
  0% {
    opacity: 0.2;
    height: 1em;
    width: 1em;
    margin-top: -0.5em;
    margin-left: -0.5em;
  }
  50% {
    opacity: 0.9;
    height: 3em;
    width: 3em;
    margin-top: -1.5em;
    margin-left: -1.5em;
  }
  100% {
    opacity: 0.2;
    height: 1em;
    width: 1em;
    margin-top: -0.5em;
    margin-left: -0.5em;
  }
}
@keyframes pulse {
  0% {
    opacity: 0.2;
    height: 1em;
    width: 1em;
    margin-top: -0.5em;
    margin-left: -0.5em;
  }
  50% {
    opacity: 0.9;
    height: 3em;
    width: 3em;
    margin-top: -1.5em;
    margin-left: -1.5em;
  }
  100% {
    opacity: 0.2;
    height: 1em;
    width: 1em;
    margin-top: -0.5em;
    margin-left: -0.5em;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="http://www.google.com" target="_blank">Link</a>
<div id="follower">
  <div id="circle1"></div>
  <div id="circle2"></div>
</div>

Answer №3

To hide your cursor when hovering over a link and maintain the animation without restarting, you can modify your script as follows:

  clickElement = (function(_this) {
    return function(event) {
      var mouse;
      mouse = {
        x: mouseX(event),
        y: mouseY(event)
      };

      var followerDisplay = follower.style.display;
      follower.style.display = "none";
      var elementAtCursor = document.elementFromPoint(mouse.x, mouse.y);
      follower.style.display = followerDisplay;
      elementAtCursor.click();
    };
  })(this);

  window.onmouseup = (function(_this) {
    return function(event) {
      var _event;
      _event = event;
      return (timer = setTimeout(function() {
        return clickElement(_event);
      }, 1));
    };
  })(this);

By using visibility:hidden instead of display:none, the cursor will remain hidden but the animation will continue smoothly.

  follower.style.visibility = "hidden";
  var elementAtCursor = document.elementFromPoint(mouse.x, mouse.y);
  follower.style.visibility = 'visible';
  elementAtCursor.click();

Answer №4

Although a bit unconventional, this method proves to be effective.

JavaScript Snippet

(function() {
  var follower, init, mouseX, mouseY, positionElement, printout, timer;

  follower = document.getElementById('follower');

  printout = document.getElementById('printout');

  mouseX = (function(_this) {
    return function(event) {
      return event.clientX;
    };
  })(this);

  mouseY = (function(_this) {
    return function(event) {
      return event.clientY;
    };
  })(this);

  positionElement = (function(_this) {
    return function(event) {
      var mouse;
      mouse = {
        x: mouseX(event),
        y: mouseY(event)
      };
      follower.style.top = mouse.y + 'px';
      return follower.style.left = mouse.x + 'px';
    };

  })(this);

  $('#follower').on('mousedown', function(){
    // Hide the #follower element
    $(this).hide(); 

    // Set the hover event on anchor tags so this immediately gets called if we're over an anchor tag
    $('a').on('mouseover', function(){
      $(this).click(); // Click the anchor tag
      $('#follower').show(); // Show the #follower element again
    })
  });

  // Unbind the anchor tag mouseover event so we don't click anchor tags by moving the mouse quickly
  $(document).on('mouseover', function() {
    $('a').unbind('mouseover').show();
  })
  
  timer = false;

  window.onmousemove = init = (function(_this) {
    return function(event) {
      var _event;
      _event = event;
      return timer = setTimeout(function() {
        return positionElement(_event);
      }, 1);
    };
  })(this);

}).call(this);

I have included this additional section:

$('#follower').on('mousedown', function(){
    // Hide the #follower element
    $(this).hide(); 

    // Set the hover event on anchor tags so this immediately gets called if we're over an anchor tag
    $('a').on('mouseover', function(){
      $(this).click(); // Click the anchor tag
      $('#follower').show(); // Show the #follower element again
    })
});

// Unbind the anchor tag mouseover event so we don't click anchor tags by moving the mouse quickly
$(document).on('mouseover', function() {      
    $('a').unbind('mouseover').show();
})

Answer №5

The reason for this issue is that the link is positioned below the circles, causing you to actually click on the circle. To resolve this problem, adjust the z-index in your CSS to be lower than the link or implement a different solution.

Example here

#follower {
  position: fixed;
  top: 50%;
  left: 50%;
  z-index: -1;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

retrieve the previous value from the scope

Attempting to create a copy of an event before any changes are made in order to implement a cancel button, I stored it in the oldValue variable. However, both the event and oldValue variables appear to be empty because they are executed before the event fa ...

External CSS file unable to load background image as referenced

I am facing an issue with this code in my external CSS file as it doesn't seem to work at all. Strangely, when I move it internally, everything functions perfectly. Any thoughts on what could be causing this discrepancy? html { background-image:url(i ...

Iterate through the classes for updates rather than focusing on individual id fields

I am currently working on creating a function that can refresh data with an AJAX call for multiple instances of a class within a single page. The function functions perfectly when there is only one instance on the page: $(document).ready(function() { ...

What is the best way to insert an image icon within an input field using bootstrap?

I am currently working on enhancing the design of my input text field by incorporating an image icon using Bootstrap. I have successfully used a font-awesome icon within the input tag as an example. Here is the example of what I have implemented: https:/ ...

What is the best way to incorporate arrowheads into the lines that have been sketched using canvas

I need assistance with incorporating arrowheads at the end of linear plots drawn on a coordinate grid using my custom function. You can view an example of the current setup in this JsFiddle: https://jsfiddle.net/zje14n92/1/ Below is the code snippet respo ...

Angular UI.Bootstrap radio buttons behave oddly when used in conjunction with ng-repeat

Having trouble dynamically generating options for a radio model using Angular's ui.bootstrap. I attempted to ng-repeat over an array to use its contents for the btn-radio attribute as shown below: //In the controller $scope.radioModel = undefined; $ ...

Unable to create a responsive design for this box

I've been attempting to place text and a button inside a box, but I'm encountering issues with fitting them on medium and small screens. So far, the methods I've tried have not been successful. Below is the code I am currently using: * { ...

Creating Custom Filters in Angular using Functions

Attempting to filter ng-repeat using a function instead of an actual filter has presented some challenges. The code snippet below demonstrates the attempt: <tr ng-repeat="(key, value) in dataObj| filter:dataFilter"> The intention is to define dataF ...

Send the function with parameters, but do not pass its output

Within my component, there is a need property that holds a static function. This function is executed by middleware to handle asynchronous API calls before rendering the component. Here's an example: static need = [ myFunc(token) ] The issue arise ...

leveraging AJAX to showcase information retrieved from a MySQL database

Hello! I am brand new to PHP and have little knowledge of AJAX. I am in the process of creating a photo gallery site and have used the following code to display my photos. I would like to make it possible, using AJAX or any other language, for someone to c ...

Retrieve the Mongoose constructor information during the pre-save function

I am working with a schema that looks like this: const CustomerSchema = new Schema({ email: { type: String, required: true, unique: true }, flags: { marketingConsent: { type: Boolean, required: true }, }, }); When creating a new customer: co ...

Tips on making anchor links using the unique ids for individual blog posts

I've encountered a challenge with what I initially believed to be a straightforward issue. I'm attempting to automate the generation of anchor links for a set of blog posts. I've been experimenting with the each() command to iterate through ...

Guide to finding the id of the clicked button in PHP

Two buttons named "removeD" and "updateRecord"(by id) have been created with the same ajax code: $.ajax({ url: 'DB/tableDisplay.php', type: 'POST', data: 'id='+uid, dataType: 'html' }) However, in t ...

If you press Ctrl + F, the browser will disable the form search function

How can I prevent the form find browser functionality when pressing Ctrl+F, and instead focus on the element html? <div id='demo'> <form class="id5-text-find-form" id="id5-text-find-form"> <input class="search" placeho ...

What does the error message "unsupported command-line flag" in Chrome mean for Protractor?

Recently jumping on the Protractor bandwagon, I found myself facing an issue while running my tests on Chrome (an error message appears below the address bar in the browser): A worrisome warning popped up saying something about '--ignore-certificat ...

What is the best way to position text on top of a deck of cards?

looking for this current status this is my code: <div class="card-deck test"> <div class="card-group mx-auto test2"> <div class="d-flex align-items-start overviewHeader"> <h2>O ...

"I am looking for a way to retrieve dynamic data from a (click) event in Angular. Can

Within my component, I have a video loaded in an i tag on a click event. The challenge is accessing the video ID from the video.component.ts file in order to dynamically load a new video. The solution has been elusive so far. <li *ngFor="let video of c ...

Create a new Chart.js Chart by using the data retrieved from an AJAX request and encoded in JSON

I am currently working on drawing a chart using the chart.js library. The initial draw works perfectly fine, but I am facing issues when trying to redraw the doughnut chart with new data retrieved from an ajax call. My approach involves PHP and Codeignite ...

Jquery Validation is not functioning properly in MVC using C#

I have been working on an MVC App and am currently trying to determine if a user is registered. To achieve this, I have created a model: public class Ejemplo { [Required(ErrorMessage="Please specify Username")] [DataType(DataType.Text)] public s ...

Is it possible to utilize the Next.js api routes to send the 404 page?

I have created an API route called /api/signin.js. I am looking to allow post requests and return a custom 404 page for any get requests that come through. I am struggling to find a solution that doesn't involve a redirect. Any suggestions or guidance ...