useElementByPoint
useElementByPoint
是一个用于获取指定坐标下的元素的 Hook。
Usage
实时编辑器
function Demo() { const { clientX: x, clientY: y } = useMouse(); const { element, pause, resume } = useElementByPoint({ x, y }); const bounding = useElementBounding(element); useEventListener("scroll", bounding.update, null, { capture: true }); const boxStyles = (() => { if (element) { return { display: "block", width: `${bounding.width}px`, height: `${bounding.height}px`, left: `${bounding.left}px`, top: `${bounding.top}px`, backgroundColor: "#3eaf7c44", transition: "all 0.05s linear", position: "fixed", pointerEvents: "none", zIndex: 9999, border: "1px solid var(--vp-c-brand)", }; } return { display: "none", }; })(); const pointStyles = (() => ({ transform: `translate(calc(${x}px - 50%), calc(${y}px - 50%))`, position: "fixed", top: 0, left: 0, pointerEvents: "none", width: "8px", height: "8px", borderRadius: "50%", backgroundColor: "#4ade80", boxShadow: "0 0 2px rgba(0,0,0,0.3)", zIndex: 999, }))(); return ( <> <div style={boxStyles} /> <div style={pointStyles} /> <div style={{ display: "flex", alignItems: "center" }}> <span style={{ marginRight: "16px" }}>X: {x}</span> </div> <div style={{ display: "flex", alignItems: "center" }}> <span style={{ marginRight: "16px" }}>Y: {y}</span> </div> <div> <button onClick={pause}>Pause</button> <button onClick={resume}>Resume</button> </div> </> ); } render(<Demo />);
结果
Loading...
API
UseElementByPoint
Returns
UseElementByPointReturn<M>
Arguments
参数名 | 描述 | 类型 | 默认值 |
---|---|---|---|
options | 配置项 | UseElementByPointOptions<M> (必填) | - |
UseElementByPointOptions
参数名 | 描述 | 类型 | 默认值 |
---|---|---|---|
x | 点的 x 坐标 | number | (() => number) (必填) | - |
y | 点的 y 坐标 | number | (() => number) (必填) | - |
document | 要查询的文档 | Document | null | - |
multiple | 是否查询多个元素 | M | - |
interval | 查询元素的间隔 | number | 'requestAnimationFrame' | - |
immediate | 是否立即查询元素 | boolean | - |
UseElementByPointReturn
参数名 | 描述 | 类型 | 默认值 |
---|---|---|---|
isSupported | 功能是否支持 | boolean (必填) | - |
element | 查询到的元素 | M extends true ? Element[] : Element | null (必填) | - |
isActive | 一个 ref,表示一个 pausable 实例是否处于激活状态 | boolean (必填) | - |
pause | 暂时暂停效果的执行 | Fn (必填) | - |
resume | 恢复效果 | Fn (必填) | - |
Fn
export type Fn = (this: any, ...args: any[]) => any;