Thank you @tuanphan. This code didn't work for me either but I figured out a way around (for anyone who is interested) :
I added an Id to my html code and changed the css code accordingly. I used the following code for js:
<script>
let btn = document.getElementById("btn");
btn.onmousemove = function (e) {
let rect = e.target.getBoundingClientRect();
let x = e.clientX - rect.left;
let y = e.clientY - rect.top;
btn.style.setProperty("--x", x + "px")
btn.style.setProperty("--y", y + "px");
};
</script>