【发布时间】:2023-01-24 03:46:35
【问题描述】:
我在我的 React 应用程序上制作了一个自定义光标,但我想在用户单击时为其设置动画。喜欢减小尺寸或类似的东西。光标位于我在 Index.js 文件中调用的组件中。我不知道如何制作一个更改游标类的 addlisterner 元素。如果有人想帮助我,我是网络开发的新手,我会很感激:)
这是自定义光标组件:
import React, { useRef } from 'react'
function CustomCursor() {
const cursorRef = useRef(null)
React.useEffect(() => {
document.addEventListener('mousemove', (event)=> {
const {clientX, clientY} = event;
const mouseX = clientX - cursorRef.current.clientWidth /2;
const mouseY = clientY - cursorRef.current.clientHeight /2;
cursorRef.current.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0)`
})
}, [])
return ( <div className='custom-cursor' ref={cursorRef}></div> ) }
export default CustomCursor
详细的css类:
.custom-cursor {
z-index: 9999;
border-radius: 50%;
width: 20px;
height: 20px;
background-color: #8c8c8cb8;
pointer-events: none;
overflow: hidden;
transform: translate(-50%, -50%);
position: fixed;
}
我真的不知道该尝试什么:/
【问题讨论】:
标签: css reactjs css-selectors mouse-cursor custom-cursor