I'm experimenting with creating a unique custom cursor using a <div>
that trails the movement of the mouse pointer.
While the current setup works smoothly, I've noticed that when scrolling down the page, the div
lags behind until the scrolling action is completed, resulting in a choppy appearance and experience.
Is there a way to prevent this issue and ensure that the <div>
accurately follows the cursor without any delays?
// variables
var $cursor = $('.custom-cursor');
// Track cursor movement
$('body').on('mousemove', function(e) {
var parentOffset = $(this).offset();
var relX = e.pageX - parentOffset.left;
var relY = e.pageY - parentOffset.top;
$cursor.css({
left: relX,
top: relY
});
});
body {
background: red;
height: 1000vh;
position: relative;
}
.custom-cursor {
position: absolute;
width: 30px;
height: 30px;
border-radius: 100%;
z-index: 9999;
background-color: yellow;
backface-visibility: hidden;
pointer-events: none;
transform: translate(-50%, -50%); // Center over cursor
transition: width .3s ease-in-out, height .3s ease-in-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div class="custom-cursor">
</div>
</body>