我想为我的公司创建一个奇特的英雄板块.我正在使用剪辑路径和一些JS来实现鼠标移动时的喷漆效果,它在VCS预览中工作得很好.然而,当我在Chrome、Mozilla或Edge等网络浏览器中try 时,它不起作用.似乎找不到光标的位置.剪辑路径会在随机的位置显示几秒钟,或者根本不显示. 更重要的是?如果是左角,没有边距:汽车;这是很好的工作,但它必须是中心.

我想要使用的页面:https://hoffmannteile.de/ 下面是我使用的代码(它必须在一个文件中,因为它是一个封闭的CMS):

document.addEventListener('DOMContentLoaded', function () {
    const cars = document.querySelectorAll('.car');
    cars.forEach(car => {
        car.addEventListener('mousemove', function (e) {
            const x = e.pageX - car.offsetLeft;
            const y = e.pageY - car.offsetTop;

            car.style.setProperty('--x', x + 'px');
            car.style.setProperty('--y', y + 'px');
        });
    });
});
.paint {
  max-height: 338px;
  max-width: 600px;
  margin: auto;
}

.malowanie {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

.cont {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

.cont .car {
  position: relative;
  height: 338px;
  width: 600px;
  background-image: url('https://composer.idosell.com/gfx/30226/SprinterWhite.png');
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

.cont .car::before {
  content: '';
  position: absolute;
  z-index: 1;
}

.cont .car::after {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--img);
  background-size: cover;
  clip-path: circle(0px at 0 0);
  z-index: 2;
  transition: clip-path 0.15s;
}

.cont .car:hover::after {
  clip-path: circle(100px at var(--x) var(--y));
  cursor: url('https://composer.idosell.com/gfx/30226/paint-spray-icon.svg'), auto;
}
<section class="paint">
    <div class="malowanie">
        <div class="cont">
            <div class="car" style="--img: url('https://composer.idosell.com/gfx/30226/SprinterRedHof.png')"></div>
        </div>
    </div>
</section>

推荐答案

使用鼠标坐标相对于clientX/YElement.getBoundingClientRect()使用lefttop的值

document.addEventListener('DOMContentLoaded', function() {
  const cars = document.querySelectorAll('.car');
  cars.forEach(car => {
    car.addEventListener('mousemove', function(e) {
      const bcr = car.getBoundingClientRect();
      const x = e.clientX - bcr.left;
      const y = e.clientY - bcr.top;
      car.style.setProperty('--x', x + 'px');
      car.style.setProperty('--y', y + 'px');
    });
  });
});
body {
  min-height: 300px; /* just to test scroll */
}

.paint {
  max-height: 338px;
  max-width: 600px;
  margin: auto;
}

.malowanie {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

.cont {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

.cont .car {
  position: relative;
  height: 338px;
  width: 600px;
  background-image: url('https://composer.idosell.com/gfx/30226/SprinterWhite.png');
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

.cont .car::before {
  content: '';
  position: absolute;
  z-index: 1;
}

.cont .car::after {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--img);
  background-size: cover;
  clip-path: circle(0px at 0 0);
  z-index: 2;
  transition: clip-path 0.15s;
}

.cont .car:hover::after {
  clip-path: circle(100px at var(--x) var(--y));
  cursor: url('https://composer.idosell.com/gfx/30226/paint-spray-icon.svg'), auto;
}
<h1>Some title to test offset</h1>
<section class="paint">
  <div class="malowanie">
    <div class="cont">
      <div class="car" style="--img: url('https://composer.idosell.com/gfx/30226/SprinterRedHof.png')"></div>
    </div>
  </div>
</section>

Javascript相关问答推荐

主要内部的ExtJS多个子应用程序

获取表格的左滚动位置

fs. writeFile()vs fs.writeFile()vs fs.appendFile()

如何在Angular中插入动态组件

Exceljs:我们在file.xlsx(...)&#中发现了一个问题'"" 39人;

将2D数组转换为图形

Regex结果包含额外的match/group,只带一个返回

JSDoc创建并从另一个文件导入类型

制作钢琴模拟器,并且在控制台中不会执行或显示该脚本

使用POST请求时,Req.Body为空

在运行时使用Next JS App Router在服务器组件中运行自定义函数

使用getBorbingClientRect()更改绝对元素位置

在VS代码上一次设置多个变量格式

无法读取未定义的属性(正在读取合并)-react RTK

使用自动识别发出信号(&Q)

图表4-堆叠线和条形图之间的填充区域

将相关数据组合到两个不同的数组中

Cherrio JS返回父div的所有图像SRC

JWT Cookie安全性

是否设置以JavaScript为背景的画布元素?