javascript - 是否可以将光标轨迹添加到网页,而不是默认轨迹,而是图片或 gif?

标签 javascript html css pointers mouse

类似于 windows 鼠标轨迹。

enter image description here

可以有自定义图片或 gif 代替默认指针 用于最有可能使用 javascript 的网页。

如果可能的话,我会怎么做呢?

编辑:感谢我试过的一些答案

http://www.javascriptkit.com/script/script2/simpleimagetrail.shtm

由于某种原因,它使“轨迹”离我的鼠标很远(忽略点轨迹)

https://i.gyazo.com/a03dd419dd4ca35e66e2b439c52e269d.mp4

最佳答案

这是针对我的网站的 Mouse Trail 的解决方案。

var dots = [],
    mouse = {
      x: 0,
      y: 0
    };

var Dot = function() {
  this.x = 0;
  this.y = 0;
  this.node = (function(){
    var n = document.createElement("div");
    n.className = "MouseTrail";
    document.body.appendChild(n);
    return n;
  }());
};

Dot.prototype.draw = function() {
  this.node.style.left = this.x + "px";
  this.node.style.top = this.y + "px";
};

for (var i = 0; i < 12; i++) {
  var d = new Dot();
  dots.push(d);
}


function draw() {

  var x = mouse.x,
      y = mouse.y;
      
  dots.forEach(function(dot, index, dots) {
    var nextDot = dots[index + 1] || dots[0];
    
    dot.x = x;
    dot.y = y;
    dot.draw();
    x += (nextDot.x - dot.x) * .6;
    y += (nextDot.y - dot.y) * .6;

  });
}

addEventListener("mousemove", function(event) {
  mouse.x = event.pageX;
  mouse.y = event.pageY;
});

function animate() {
  draw();
  requestAnimationFrame(animate);
}

animate();
.MouseTrail {
    position: absolute;
    height: 7px; width: 7px;
    border-radius: 4px;
    background: teal;
  }

关于javascript - 是否可以将光标轨迹添加到网页,而不是默认轨迹,而是图片或 gif?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48161503/

相关文章:

javascript - 如何让我的笑脸按钮打开我创建的弹出窗口?

javascript - 将文本复制到具有相同 ID 的两个输入

JavaScript 问题和 IE(style.display 属性)

javascript - 使用 'a' 以 HTML 格式提交表单无效

css - 移动版本上不同顺序的 Bootstrap

javascript - 从 Safari 扩展将 jQuery 注入(inject)网页

php - 向Mysql数据库中插入数据

html - 如何在显示浏览器滚动条的网页中保持对齐?

css - 同一个目标过渡两次 (CSS3)

html - 为什么 YSlow 在 Head 部分声明了 Css 仍然在 body 部分?