我已经成功地组装了一个定制游标,当悬停在不同的数据类型上时,它会改变.在本例中,当您将光标悬停在第一张图像上时,光标将更改为暂停图标,当您将光标悬停在第二张图像上时,光标将更改为播放图标,我想将播放图标更改为文本"Play"而不是图标.

const cursor = document.getElementById("cursor");

const animateCursor = (e, interacting) => {
  const x = e.clientX - cursor.offsetWidth / 2,
        y = e.clientY - cursor.offsetHeight / 2;
  
  const keyframes = {
    transform: `translate(${x}px, ${y}px) scale(${interacting ? 8 : 1})`
  }
  
  cursor.animate(keyframes, { 
    duration: 800, 
    fill: "forwards" 
  });
}

const getCursorClass = type => {
  switch(type) {
    case "video":
      return "fa-solid fa-play";
      case "image":
      return "fa-solid fa-pause";
    default:
      return "fa-solid fa-arrow-up-right"; 
  }
}

window.onmousemove = e => {
  const interactable = e.target.closest(".interactable"),
        interacting = interactable !== null;
  
  const icon = document.getElementById("cursor-icon");
  
  animateCursor(e, interacting);
  
  cursor.dataset.type = interacting ? interactable.dataset.type : "";
  
  if(interacting) {
    icon.className = getCursorClass(interactable.dataset.type);
  }
}
body {
  background-color: rgb(20, 20, 20);
  height: 100vh;    
  margin: 0px;  
  
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 4vw, 100px);
}

body:hover > #cursor {
  opacity: 1;
}

#cursor {
  height: 20px;
  width: 20px;
  background-color: white;
  border-radius: 20px;
  
  position: fixed;
  left: 0px;
  top: 0px;
  z-index: 10000;
  
  pointer-events: none;
  opacity: 0;
  transition: opacity 500ms ease;
  
  display: grid;
  place-items: center;
}

#cursor:not([data-type=""]) > #cursor-icon {
  opacity: 1;
}

#cursor-icon {
  font-size: 6px;
  line-height: 0px;
  
  opacity: 0;
  transition: opacity 400ms ease;
}

.interactable {
  aspect-ratio: 1 / 1.5;
  width: clamp(120px, 40vmin, 600px);
  background-position: center 50%;
  background-size: 100%;  
  opacity: 0.4;

  
  transition: background-size 400ms ease, opacity 400ms ease;
}

.interactable:hover {
  background-size: 105%;
  opacity: 0.8;
}
<script src="https://kit.fontawesome.com/944eb371a4.js"></script>
<div id="cursor">
  <i id="cursor-icon" class="fa-solid fa-arrow-up-right"></i>
</div>

<div 
  class="interactable" 
  data-type="image"
  style="background-image: url(https://images.unsplash.com/photo-1657739774592-14c8f97eaece?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwyfHx8ZW58MHx8fHw%3D&auto=format&fit=crop&w=500&q=60)">
</div>

<div 
  class="interactable" 
  data-type="video"
  style="background-image: url(https://images.unsplash.com/photo-1657779582398-a13b5896ff19?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwzNXx8fGVufDB8fHx8&auto=format&fit=crop&w=500&q=60)">     
</div>

推荐答案

css文件中添加一个名为play的新类

.play::after {
  content: "Play";
  font-style: normal;
}

然后在JS文件的以下部分添加新的类名.

const getCursorClass = type => {
  switch(type) {
    case "video":
      return "play"; // <--- add the class name here
      case "image":
      return "fa-solid fa-pause";
    default:
      return "fa-solid fa-arrow-up-right"; 
  }
}

Codepen URL

Javascript相关问答推荐

React redux状态未在React-Router库中呈现

在shiny 模块中实现JavaScript

调用SEARCH函数后,程序不会结束

根据总价格对航班优惠数组进行排序并检索前五个结果- Angular HTTP请求

类型脚本中只有字符串或数字键而不是符号键的对象

使用复选框在d3.js多折线图中添加或删除线条

将旋转的矩形zoom 到覆盖它所需的最小尺寸&S容器

如何使用子字符串在数组中搜索重复项

Chart.js-显示值应该在其中的引用区域

未定义引用错误:未定义&Quot;而不是&Quot;ReferenceError:在初始化&Quot;之前无法访问';a';

当使用';字母而不是与';var#39;一起使用时,访问窗口为什么返回未定义的?

用于在路径之间移动图像的查询

Web Crypto API解密失败,RSA-OAEP

为什么JPG图像不能在VITE中导入以进行react ?

Vaadin定制组件-保持对javascrip变量的访问

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

使用RxJS from Event和@ViewChild vs KeyUp事件和RxJS主题更改输入字段值

React Refs不与高阶组件(HOC)中的动态生成组件一起工作

在将元素追加到DOM之前,createElement()是否会触发回流?混淆abt DocumentFragment行为

Firefox的绝对定位没有达到预期效果