Home > Blockchain >  Custom cursor not growing/scaling on hover (can't figure it out)
Custom cursor not growing/scaling on hover (can't figure it out)

Time:04-04

Hellol,I have a code for a custom cursor, and the cursor, which is a ball/circle, was supposed to grow/scale when hovering over a link, if you see the code below, this function is there, but it is not working, does anyone know what's wrong? Thank you in advance. Note, I am unable to create a snippet here. The code is from codepen: https://codepen.io/clementGir/pen/RQqvQx

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

    <div >
        <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', onm ouseMove);
    for (let i = 0; i < $hoverables.length; i  ) {
        if (window.CP.shouldStopExecution(0)) break;
        $hoverables[i].addEventListener('mouseenter', onm ouseHover);
        $hoverables[i].addEventListener('mouseleave', onm ouseHoverOut);
    }

    // Move the cursor
    window.CP.exitedLoop(0); function onm ouseMove(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 onm ouseHover() {
        TweenMax.to($bigBall, .3, {
            scale: 4
        });
    }
    function onm ouseHoverOut() {
        TweenMax.to($bigBall, .3, {
            scale: 1
        });
    }
</script>```


Growing cursor on hovering a link.

CodePudding user response:

The codepen you are trying to copy links to external scripts and has been adapted to work inside codepen.

What I did to get the pen to work here (I think you only needed step 4):

  1. For CSS & JS clicked down arrow top right, view compiled
  2. Copy over all code
  3. Settings>JS>External Scripts>Copy link into Stack Overflow snippet's external scripts
  4. Removed 'window.CP' code junk which I assume is something to do with codepen and getting the window object of their 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', onm ouseMove);
for (let i = 0; i < $hoverables.length; i  ) {
  $hoverables[i].addEventListener('mouseenter', onm ouseHover);
  $hoverables[i].addEventListener('mouseleave', onm ouseHoverOut);
}

// Move the cursor
function onm ouseMove(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 onm ouseHover() {
  TweenMax.to($bigBall, .3, {
    scale: 4 });

}
function onm ouseHoverOut() {
  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 >
  <div >
    <svg height="30" width="30">
      <circle cx="15" cy="15" r="12" stroke-width="0"></circle>
    </svg>
  </div>

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

<div >
  <h1>Hello</h1>
  <p>Check out this link:</p>
  <a >Hover meh</a>
</div>

<div >
  <h1>Hello</h1>
  <p>Check out this link:</p>
  <a >Hover meh</a>
</div>

  • Related