I am trying to create a custom cursor using a <div>
that is set to follow the cursor.
In the snippet below this is working really well, however when scrolling down the page, the div
does not follow until the scroll movement is complete, resulting in a jerky movement and feel.
Is there a way to ensure this doesn't happen and the <div>
follows the cursor perfectly regardless?
// vars
var $cursor = $('.custom-cursor');
// Follow cursor
$('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>