Реализация анимации курсора (Custom Cursor) на сайте
Кастомный курсор — один из тех элементов, где разница между «сделали» и «сделали хорошо» видна сразу. Плохая реализация даёт ощущение тяжести: курсор запаздывает, дёргается, конфликтует с нативными состояниями браузера. Правильная — работает незаметно, усиливает характер интерфейса.
Анатомия кастомного курсора
Типовая структура: два элемента — точка (dot), которая следует за мышью без задержки, и кольцо (ring/follower), которое тянется с lerp-эффектом. Вместо cursor: none на весь документ — скрываем только там, где это нужно.
* {
cursor: none;
}
.cursor-dot {
position: fixed;
width: 8px;
height: 8px;
border-radius: 50%;
background: #fff;
pointer-events: none;
z-index: 9999;
transform: translate(-50%, -50%);
will-change: transform;
}
.cursor-ring {
position: fixed;
width: 36px;
height: 36px;
border-radius: 50%;
border: 1.5px solid rgba(255, 255, 255, 0.6);
pointer-events: none;
z-index: 9998;
transform: translate(-50%, -50%);
will-change: transform;
}
Логика движения
Анимация через requestAnimationFrame с линейной интерполяцией для follower. Позиция dot обновляется напрямую через mousemove — без lerp, иначе теряется ощущение точности.
interface CursorState {
mouse: { x: number; y: number }
follower: { x: number; y: number }
isHovering: boolean
isVisible: boolean
}
class CustomCursor {
private dot: HTMLElement
private ring: HTMLElement
private state: CursorState
private rafId: number | null = null
private readonly LERP = 0.12
constructor() {
this.dot = document.querySelector('.cursor-dot')!
this.ring = document.querySelector('.cursor-ring')!
this.state = {
mouse: { x: -100, y: -100 },
follower: { x: -100, y: -100 },
isHovering: false,
isVisible: false,
}
this.init()
}
private init() {
document.addEventListener('mousemove', this.onMouseMove)
document.addEventListener('mouseenter', this.onMouseEnter)
document.addEventListener('mouseleave', this.onMouseLeave)
// Hover-состояния для интерактивных элементов
document.querySelectorAll('a, button, [data-cursor]').forEach((el) => {
el.addEventListener('mouseenter', this.onElementEnter)
el.addEventListener('mouseleave', this.onElementLeave)
})
this.tick()
}
private onMouseMove = (e: MouseEvent) => {
this.state.mouse.x = e.clientX
this.state.mouse.y = e.clientY
// Dot следует без задержки через CSS transform
this.dot.style.left = `${e.clientX}px`
this.dot.style.top = `${e.clientY}px`
}
private tick = () => {
// Follower с lerp
this.state.follower.x += (this.state.mouse.x - this.state.follower.x) * this.LERP
this.state.follower.y += (this.state.mouse.y - this.state.follower.y) * this.LERP
this.ring.style.left = `${this.state.follower.x}px`
this.ring.style.top = `${this.state.follower.y}px`
this.rafId = requestAnimationFrame(this.tick)
}
private onElementEnter = (e: Event) => {
const target = e.currentTarget as HTMLElement
const cursorType = target.dataset.cursor || 'hover'
this.setState('hover', cursorType)
}
private onElementLeave = () => {
this.setState('default')
}
private setState(state: string, type?: string) {
this.ring.className = `cursor-ring cursor-ring--${state}`
if (type) this.ring.dataset.cursorType = type
}
destroy() {
if (this.rafId) cancelAnimationFrame(this.rafId)
document.removeEventListener('mousemove', this.onMouseMove)
}
}
Состояния курсора
Стандартный набор: default, hover (на ссылках), active (при клике), text (на параграфах), drag (на слайдерах), view (на медиа). Переключение через CSS-классы и data-атрибуты.
/* Hover на кнопках — увеличение с заливкой */
.cursor-ring--hover {
width: 52px;
height: 52px;
background: rgba(255, 255, 255, 0.1);
border-color: transparent;
transition: width 0.25s ease, height 0.25s ease, background 0.25s ease;
}
/* Текстовый режим — вертикальная черта */
.cursor-ring--text {
width: 2px;
height: 28px;
border-radius: 1px;
background: #fff;
border: none;
}
/* View/play — курсор с текстом */
.cursor-ring[data-cursor-type="view"]::after {
content: 'VIEW';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.1em;
color: #fff;
}
Интеграция с React
В React-проектах курсор реализуется как глобальный компонент через Context или store. Важно: монтировать только один раз, не перерендеривать при каждом движении мыши.
import { useEffect, useRef, useCallback } from 'react'
import { useMotionValue, useSpring, motion } from 'framer-motion'
export function CustomCursor() {
const mouseX = useMotionValue(-100)
const mouseY = useMotionValue(-100)
// Spring для follower — альтернатива ручному RAF+lerp
const springConfig = { damping: 25, stiffness: 200, mass: 0.5 }
const followerX = useSpring(mouseX, springConfig)
const followerY = useSpring(mouseY, springConfig)
useEffect(() => {
const onMove = (e: MouseEvent) => {
mouseX.set(e.clientX)
mouseY.set(e.clientY)
}
window.addEventListener('mousemove', onMove)
return () => window.removeEventListener('mousemove', onMove)
}, [])
// Скрываем на touch-устройствах
const isTouchDevice = window.matchMedia('(hover: none)').matches
if (isTouchDevice) return null
return (
<>
<motion.div
className="cursor-dot"
style={{ x: mouseX, y: mouseY, translateX: '-50%', translateY: '-50%' }}
/>
<motion.div
className="cursor-ring"
style={{ x: followerX, y: followerY, translateX: '-50%', translateY: '-50%' }}
/>
</>
)
}
Нативный курсор как fallback
На тач-устройствах кастомный курсор отключается полностью. cursor: none применяется только при (hover: hover) and (pointer: fine):
@media (hover: hover) and (pointer: fine) {
* { cursor: none; }
.cursor-dot, .cursor-ring { display: block; }
}
@media (hover: none), (pointer: coarse) {
.cursor-dot, .cursor-ring { display: none; }
}
Типичные грабли
Мерцание при быстром движении — dot и ring рендерятся в разных слоях. Решение: оба элемента в одном stacking context, will-change: transform на каждом.
Конфликт с iframe — при уходе курсора в iframe событие mousemove не стреляет. Нужно отслеживать mouseleave на document и скрывать курсор.
Задержка CSS transitions — если на ring стоит transition без исключения left/top, follower теряет живость. Переходы только для scale/opacity/color, позиция — через transform без transition.
Сроки
Базовая реализация с dot + ring и hover-состояниями — 1 день. С анимацией текста, drag-курсором для слайдеров, интеграцией в существующий React-проект и полным набором состояний — 2–3 дня.







