-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
81 lines (72 loc) · 2.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const $$ = s =>
Array.prototype.slice.call(
document.querySelectorAll(s)
)
const isEl = obj => obj instanceof HTMLElement
const isStr = obj => Object.prototype.toString.call(obj) === '[object String]'
const cursorDot = ({
zIndex = 1,
diameter = 80,
borderWidth = 1,
borderColor = '#ddd',
easing = 4,
background = 'transparent'
} = {}) => {
let inited = false
const alt = { x: 0, y: 0, o: 1, d: diameter }
const cur = { x: 0, y: 0, o: 0, d: diameter }
const dot = document.createElement('div')
const tim = easing / 15
dot.style = `position:fixed;top:0;left:0;border-radius:100%;pointer-events:none;opacity:0;z-index:${zIndex};height:${diameter}px;width:${diameter}px;background:${background};border:${borderWidth}px solid ${borderColor};mix-blend-mode:exclusion;transition:background ${tim}s,border ${tim}s;will-change:transform`
document.addEventListener('mousemove', e => {
alt.x = e.clientX
alt.y = e.clientY
dot.style.opacity = 1
if (!inited) {
document.body.append(dot)
cur.x = alt.x
cur.y = alt.y
inited = true
draw()
}
})
const draw = () => {
const dX = alt.x - cur.x
const dY = alt.y - cur.y
cur.x += (dX / easing)
cur.y += (dY / easing)
const t3d = `translate3d(${cur.x - cur.d / 2}px,${cur.y - cur.d / 2}px,0)`
dot.style.webkitTransform = t3d
dot.style.transform = t3d
const dO = alt.o - cur.o
cur.o += dO / easing
dot.style.opacity = cur.o
const dD = alt.d - cur.d
cur.d += dD / easing
dot.style.height = cur.d + 'px'
dot.style.width = cur.d + 'px'
try {
requestAnimationFrame(draw)
} catch (_) {
setImmediate(draw)
}
}
dot.over = (any, style) => {
const fn = el => {
el.addEventListener('mouseover', _ => {
if (style.background) dot.style.backgroundColor = style.background
if (style.borderColor) dot.style.borderColor = style.borderColor
if (style.scale) alt.d = diameter * style.scale
})
el.addEventListener('mouseout', _ => {
if (style.background) dot.style.backgroundColor = background
if (style.borderColor) dot.style.borderColor = borderColor
if (style.scale) alt.d = diameter
})
}
if (isEl(any)) fn(any)
else if (isStr(any)) $$(any).forEach(fn)
}
return dot
}
module.exports = cursorDot