小编Ale*_*ski的帖子

尝试在 javascript 中应用 CSS 转换

在我的页面上,我有一个跟随用户光标的元素。我想要做的是使用普通的 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)

html javascript css css-transforms

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

css ×1

css-transforms ×1

html ×1

javascript ×1