在我的页面上,我有一个跟随用户光标的元素。我想要做的是使用普通的 javascript来transform代替top/ left。没有任何库或依赖项...问题是我需要将存储在变量中的值应用到转换属性。我没有找到任何可以帮助我的东西......这是我的代码:
var cursor = document.getElementById('cursor');
document.body.addEventListener("mousemove", function(e) {
//storing cursor position as variables
var curX = e.clientX;
var curY = e.clientY;
// I need the code below to be replaced with transform-translate instead of top/left
// I can not get this to work with any other method than top/left
cursor.style.left = curX - 7 + 'px';
cursor.style.top = curY - 7 + 'px';
});Run Code Online (Sandbox Code Playgroud)
body {
background: orange;
height: 500px;
width: 500px;
} …Run Code Online (Sandbox Code Playgroud)