我正在尝试编写一些 Vanilla Javascript 来使元素跟随我的鼠标移动。我使用了 clientX、clientY 和 mousemove 事件使其跟随,但是当我滚动页面时,元素不会随鼠标移动。我想也许我需要使用滚动事件,但我正在努力使它工作。任何帮助都会很棒,谢谢!
document.addEventListener('mousemove', (e) => {
const mouseFollow = document.getElementById('mouse-follow');
const x = e.clientX - 25; //-25 to center div over mouse
const y = e.clientY - 25;
mouseFollow.style.top = `${y}px`;
mouseFollow.style.left = `${x}px`;
})
繁星点点滴滴
天涯尽头无女友
相关分类