Struggling to find the solution as to why my custom cursor is not increasing in size when hovering over it

Hey there, I've encountered an issue with a custom cursor code. The cursor is designed to be a ball/circle that grows or scales when hovering over a link. However, despite having the function in the code below, it doesn't seem to be working properly. Can anyone pinpoint what might be causing this? Your help would be greatly appreciated. Unfortunately, I'm unable to provide a code snippet here, but you can find the original code on Codepen: https://codepen.io/clementGir/pen/RQqvQx

<div class="cursor">
    <div class="cursor__ball cursor__ball--big ">
        <svg height="30" width="30">
            <circle cx="15" cy="15" r="12" stroke-width="0"></circle>
        </svg>
    </div>

    <div class="cursor__ball cursor__ball--small">
        <svg height="10" width="10">
            <circle cx="5" cy="5" r="4" stroke-width="0"></circle>
        </svg>
    </div>
</div>

<style>
    body .cursor {
        pointer-events: none;
    }

    body .cursor__ball {
        position: fixed;
        top: 0;
        left: 0;
        mix-blend-mode: difference;
        z-index: 1000;
    }

    body .cursor__ball circle {
        fill: #f7f8fa;
    }
</style>

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.3/TweenMax.min.js"></script>

<script>
    const $bigBall = document.querySelector('.cursor__ball--big');
    const $smallBall = document.querySelector('.cursor__ball--small');
    const $hoverables = document.querySelectorAll('a');

    // Listeners
    document.body.addEventListener('mousemove', onMouseMove);
    for (let i = 0; i < $hoverables.length; i++) {
        if (window.CP.shouldStopExecution(0)) break;
        $hoverables[i].addEventListener('mouseenter', onMouseHover);
        $hoverables[i].addEventListener('mouseleave', onMouseHoverOut);
    }

    // Move the cursor
    window.CP.exitedLoop(0); function onMouseMove(e) {
        TweenMax.to($bigBall, .4, {
            x: e.clientX - 15,
            y: e.clientY - 15
        });

        TweenMax.to($smallBall, .1, {
            x: e.clientX - 5,
            y: e.clientY - 7
        });
    }

    // Hover an element
    function onMouseHover() {
        TweenMax.to($bigBall, .3, {
            scale: 4
        });
    }
    function onMouseHoverOut() {
        TweenMax.to($bigBall, .3, {
            scale: 1
        });
    }
</script>

Answer №1

Adapting an External Script to Work in CodePen

If you are trying to copy a CodePen that links to external scripts and need to make it work within CodePen, follow these steps:

  1. To include CSS & JS, click the down arrow at the top right and select "view compiled"
  2. Copy all the code from there
  3. In Settings > JS > External Scripts, paste the link into Stack Overflow snippet's external scripts
  4. Remove any 'window.CP' code, which is likely specific to CodePen's sub-document

const $bigBall = document.querySelector('.cursor__ball--big');
const $smallBall = document.querySelector('.cursor__ball--small');
const $hoverables = document.querySelectorAll('.hoverable');

// Listeners
document.body.addEventListener('mousemove', onMouseMove);
for (let i = 0; i < $hoverables.length; i++) {
  $hoverables[i].addEventListener('mouseenter', onMouseHover);
  $hoverables[i].addEventListener('mouseleave', onMouseHoverOut);
}

// Move the cursor
function onMouseMove(e) {
  TweenMax.to($bigBall, .4, {
    x: e.pageX - 15,
    y: e.pageY - 15 });

  TweenMax.to($smallBall, .1, {
    x: e.pageX - 5,
    y: e.pageY - 7 });

}

// Hover an element
function onMouseHover() {
  TweenMax.to($bigBall, .3, {
    scale: 4 });

}
function onMouseHoverOut() {
  TweenMax.to($bigBall, .3, {
    scale: 1 });

}
body {
  height: 100vh;
  background: #010101;
  cursor: none;
  margin: 0;
  display: flex;
  font-family: monospace;
}
body h1,
body p,
body a {
  color: #fff;
}
body a {
  border-bottom: 2px solid #fff;
  padding: 10px 0;
  margin-top: 25px;
}
body .cursor {
  pointer-events: none;
}
body .cursor__ball {
  position: fixed;
  top: 0;
  left: 0;
  mix-blend-mode: difference;
  z-index: 1000;
}
body .cursor__ball circle {
  fill: #f7f8fa;
}
body .left,
body .right {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
body .right {
  background: #fff;
}
body .right a {
  border-bottom: 2px solid #000;
}
body .right h1,
body .right p,
body .right a {
  color: #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.3/TweenMax.min.js"></script>
<div class="cursor">
  <div class="cursor__ball cursor__ball--big ">
    <svg height="30" width="30">
      <circle cx="15" cy="15" r="12" stroke-width="0"></circle>
    </svg>
  </div>

  <div class="cursor__ball cursor__ball--small">
    <svg height="10" width="10">
      <circle cx="5" cy="5" r="4" stroke-width="0"></circle>
    </svg>
  </div>
</div>

<div class="left">
  <h1>Hello</h1>
  <p>Check out this link:</p>
  <a class="hoverable">Hover meh</a>
</div>

<div class="right">
  <h1>Hello</h1>
  <p>Check out this link:</p>
  <a class="hoverable">Hover meh</a>
</div>

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

Site's design compromised by AJAX refresh intervention

Every time I run this code, the page ends up repeating itself. Adding to that, it doesn't refresh as required. Here is how it should work: Image Updated code segment below: <?php include 'config.php' ?> <script language="javascr ...

Is there a way to invoke a template literal tag function on a standard string?

Suppose I have a template literal tag function foo, which allows me to use it like this: const fooTaggedText = foo`some text`; Can I somehow invoke that tag on a normal string? For example: // This doesn't work as expected const fooTaggedText = foo ...

What do you call a JavaScript function when it has a name

This code is confusing to me. It's not the usual JavaScript syntax for a function that I know of. Is this a specific function? Or perhaps it's a callback for when an update event occurs? Apologies for these beginner questions, as I am quite new t ...

Could you provide instructions on how to begin with the MEAN stack?

I recently decided to dive into learning about the Mean stack and stumbled upon the Mean.io website at . I came across some commands in the "quick install" section, but I'm not sure where to input these commands. Is there a tutorial available to guide ...

A guide on how to stop the loading animation and transition to a different slide using HTML and CSS

Check out this CSS code snippet @import url(https://fonts.googleapis.com/css?family=Quattrocento+Sans); @import url(https://fonts.googleapis.com/css?family=Quattrocento+Sans); .loading { position: fixed; top: 0; left: 0; width: 100%; height: ...

My website layout got messed up because of Chrome version 48

Once upon a time, I created a website. Despite its outdated design, it has been functioning properly. Even though I know it's time for something new, I've hesitated to make any changes because it still works. Surprisingly, parts of the page layo ...

Is there a way to pass a token variable from the main page to an iframe without relying on a post message?

I am seeking assistance on how to transfer a variable from a parent window to an iframe. My situation is as follows: I am working with 2 Angular5 applications named A and B . Application B is loaded within Application A using an iframe. The aut ...

When combining the ShaderMaterial with WebGLRenderer's logarithmicDepthBuffer feature enabled

In my scene, I apply the ShaderMaterial shown below to my objects. It works fine. However, when I enable the WebGLRenderer option logarithmicDepthBuffer to true, the Material defined is no longer displayed correctly. new THREE.ShaderMaterial({ uniform ...

Utilize JavaScript to mimic the submission of a form with a function call

Using jQuery, we have the ability to mimic form submission: <form id="form1" method="post"> <input name="key1" value="value1" /> <input name="key2" value="value2" /> </form> By making an AJAX function call: $.post('& ...

What is the reason for this JavaScript code not activating the input field (aspx)?

Thank you for taking the time to review this. I understand that for most of you client side scripting experts, this task is a breeze. However, all I am trying to achieve is to display a message at the top notifying the user that their click has been regist ...

Adding HTML after the last item in an ng-repeat directive in AngularJS

After incorporating the Instagram API to generate a Load More Button for displaying recent media, I am facing an issue where it overlaps with the ng-repeat and fails to append a new line after the last ng-repeat. I would be grateful for any assistance. Th ...

Group by month and calculate the total sum using mongoose in MongoDB

I'm currently working with the orderdetails model in my project. Here is a snippet of the schema: const model = new Schema( { district: { type: String }, category: String, producer: String, variety: String, qty: String, price ...

Exploring the concept of returning objects in jQuery

I'm really trying to grasp the inner workings of how jQuery creates the return object when searching for DOM elements. I've delved into the source code, but I must admit that it's not entirely clear to me yet. So, I'm reaching out here ...

dynamic display carousel

Utilizing technology Bootstrap CSS JS Django Python The Issue at Hand I am facing a challenge with declaring the container-fluid and ensuring that the carousel slide occupies the entire screen. However, I have noticed that there is some space remaining ...

Creating product cards with equal heights in Bootstrap 5 is a simple and effective way

Any ideas on how to achieve equal height for product cards? Flex doesn't seem to be doing the trick. .product-card { position: relative; padding: 1px; display: -ms-flexbox; display: flex; -ms-flex-direction: column; flex-direction: column; ...

Reconfigure the code to process object data instead of an array in JavaScript

Recently, I wrote a piece of code that is capable of exporting data into a CSV file. The data format it reads is structured as follows: var data = [ ['one', 'one is the first'], ['two', 'two is the second'], ...

What is the best way to create a Laravel object for use in JavaScript in a Laravel Blade?

please add description hereI am looking to obtain an object with this particular style var zoz2= { "11-30--0001": "<a href=\"https:\/\/www.dooz.ps\/p\/107229\" >\u0625\u0637\u0644\u0627& ...

How can I ensure that my image remains responsive and aspect ratio-friendly?

Is there a way to accomplish this magical effect? I find myself in a similar situation. I require an image that remains centered while adjusting to fit the screen, all while maintaining its aspect ratio. ...

What is the reason behind Chrome sending two http requests when Firefox only sends one?

Edit: It seems that the issue with the double response is related to a favicon.ico problem in Chrome but not in Firefox. Can anyone shed light on why this inconsistency occurs between the two browsers? Original: Currently, I am delving into learning Expre ...

Sending a PHP error back in response to an Ajax call

My PHP script needs to be called with AJAX, but I keep getting an error message that says "Error:email=my_email&password=myPassword". Check out the PHP script below: <?php session_start(); require "db_config.php"; if ($_SERVER["request_method"] ...