【问题标题】:How to make a mouse trail with CSS/JS so that elements are still clickable?如何使用 CSS/JS 制作鼠标轨迹以使元素仍然可点击?
【发布时间】:2020-11-20 14:11:13
【问题描述】:

我想在鼠标移动时有一个鼠标轨迹。路径应该由小图标/图像构建,随着时间的推移会失去容量。 (为简单起见,我删除了演示中的容量转换代码)我试图产生这种效果,创建一个位于所有其他元素之上的网格。这个网格由带有背景图像的小 div 组成。每个 div 的不透明度设置为 0。视觉上该解决方案有效,但整个页面上的元素不再可点击。怎么可能有相同的视觉效果,而页面上的所有元素仍然可以点击?

我试图使可点击元素的 z-index 高于网格元素的 z-index。然后这些元素是可点击的。但是后来他们身上并没有出现尾迹效应,这不是它应该的样子。

我研究了解决方案并在下面看到了这些页面/教程,但它们没有为我的问题提供解决方案:

https://medium.com/@dailyfire/cursor-trails-3-simple-css-tricks-to-add-to-any-website-part-1-64750798583c

https://www.youtube.com/watch?v=rfpRZ2t_BrQ

Javascript\Jquery mouse cursor - Inconsistencies when hovering items

const body = document.getElementsByTagName("body")[0];
const mouseMovementContainer = document.createElement("div");

mouseMovementContainer.setAttribute("id", "mouseMovementContainer");
mouseMovementContainer.classList.add("mouseMovement");

body.insertBefore(mouseMovementContainer, body.firstChild);

createNewGrid();

function createNewGrid () {
    for(let i = 0; i <= 1000; i++){
        const square = createOneSquare();
        mouseMovementContainer.appendChild(square);
    }
}

function createOneSquare () {
    const div = document.createElement("div");
    div.classList.add("square");
    return div;
}
a{
  font-size: 40px;
}

.mouseMovement {
  background-color: transparent;
  position: fixed;
  z-index: 100;
  display:flex;
  flex-wrap: wrap;
}

.square {
  width: 40px;
  height: 40px;
  background-image: url("https://icon-library.com/images/fire-icon-free/fire-icon-free-1.jpg");
  background-size: cover;
  opacity: 0;
}

.square:hover {
  opacity: 1;
}
&lt;a href="https://www.google.com/"&gt;Go to Google!!!!&lt;/a&gt;

【问题讨论】:

  • pointer-events: none; 添加到相关元素 (CSS)。
  • @ChrisG 设置指针事件不起作用,因为 mousemove 也被删除了
  • @mplungjan 在这种非常特殊的情况下,可能,但创建鼠标轨迹只需要将该事件分配给文档,不是吗?
  • @ChrisG 将该事件分配给文档是什么意思?如果我理解你的建议是正确的,那么它仍然不起作用。因为添加到文档中的元素将位于文档顶部,并且鼠标悬停在它们上方时仍不会显示轨迹。

标签: javascript css


【解决方案1】:

设置 z-index 有效

设置指针事件不起作用,因为 mousemove 也被移除了

.mouseMovement {
  background-color: transparent;
  position: fixed;
  z-index: 100;
  display:flex;
  flex-wrap: wrap;
  z-index: -1;  
}

const body = document.getElementsByTagName("body")[0];
const mouseMovementContainer = document.createElement("div");

mouseMovementContainer.setAttribute("id", "mouseMovementContainer");
mouseMovementContainer.classList.add("mouseMovement");

body.insertBefore(mouseMovementContainer, body.firstChild);

createNewGrid();

function createNewGrid () {
    for(let i = 0; i <= 1000; i++){
        const square = createOneSquare();
        mouseMovementContainer.appendChild(square);
    }
}

function createOneSquare () {
    const div = document.createElement("div");
    div.classList.add("square");
    return div;
}
a {
  font-size: 40px;
}

.mouseMovement {
  background-color: transparent;
  position: fixed;
  z-index: 100;
  display:flex;
  flex-wrap: wrap;
  z-index: -1;  
}

.square {
  width: 40px;
  height: 40px;
  background-image: url("https://icon-library.com/images/fire-icon-free/fire-icon-free-1.jpg");
  background-size: cover;
  opacity: 0;

}

.square:hover {
  opacity: 1;
}
&lt;a href="https://www.google.com/"&gt;Go to Google!!!!&lt;/a&gt;

【讨论】:

  • 是的,设置 z-index 是一种解决方案。我已经想通了。但随后轨迹效果不再显示在可点击元素上。在这个小例子中似乎没什么大不了的。但我有例如也是一个视频库,其中整个视频元素都应该是可点击的。因此,使用此解决方案只会在小范围内看到尾迹效应。
【解决方案2】:

设置pointer-events:none;允许你点击“通过”div到下面的内容。

【讨论】:

  • 但在这种情况下关闭所有路径
  • 这很不幸。希望其他解决方案之一对您有用。
  • 不是我的问题
  • 是的,点击适用于此解决方案,但如前所述,轨迹效果不再存在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-16
  • 1970-01-01
  • 2016-11-27
  • 2021-04-07
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
相关资源
最近更新 更多