My jQuery code creates a span in the shape of a circle following my cursor, but I saw another website that uses transform: translate()
instead of top
and left
properties for smoother cursor movement. Can someone help me modify the code to use transform: translate()
?
JSFiddle: https://jsfiddle.net/oj70y5Lm/
CSS, JQuery and HTML
jQuery(document).ready(function() {
var mouseX = 0, mouseY = 0;
var xp = 0, yp = 0;
$(document).mousemove(function(e){
mouseX = e.pageX - 20;
mouseY = e.pageY - 20;
});
setInterval(function(){
xp += ((mouseX - xp)/3);
yp += ((mouseY - yp)/3);
$("#circle").css({ transform: 'translate(' + xp +'px, ' + yp + 'px)' });
}, 20);
});
body, html {
position: relative;
height: 100%;
width : 100%;
margin: 0;
}
.circle {
position: absolute;
background: #000000;
opacity: .075;
width: 40px;
height: 40px;
border-radius: 50%;
pointer-events: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="circle" class="circle"></span>