I’ve a problem with a custom cursor created with an absolute div, with my test I realized that the custom div is directly positioned under the default cursor, then if I go hover a link I can’t process my JS “mouseenter” because the default cursor is always hover only to the custom cursor… there is a way to fix it?
JavaScript
x
2
1
<div class="custom-cursor"></div>
2
Scss:
JavaScript
1
17
17
1
.custom-cursor {
2
width: 20px;
3
height: 20px;
4
border: 2px solid orange;
5
position: absolute;
6
z-index: 1080;
7
border-radius: 50%;
8
transition-duration: 100ms;
9
transition-timing-function: ease-out;
10
box-shadow: 0 0 0 2px black;
11
&.hover {
12
width: 40px;
13
height: 40px;
14
background: rgba(#FFCC00,.5);
15
}
16
}
17
Vanilla JS:
JavaScript
1
22
22
1
const cursor = document.querySelector('.custom-cursor');
2
3
// Custom cursor follow the default cursor
4
document.addEventListener('mousemove', e => {
5
cursor.setAttribute('style', 'top: '+(e.pageY - 10)+'px; left: ' +(e.pageX - 10)+'px;')
6
});
7
8
const links = document.querySelectorAll('a');
9
10
// Custom cursor change style on hover links
11
for(let x of links) {
12
13
x.addEventListener('mousenter', () => {
14
cursor.classList.add('hover');
15
});
16
17
x.addEventListener('mouseleave', () => {
18
cursor.classList.remove('hover');
19
});
20
21
});
22
Advertisement
Answer
You can use pointer-events: none;
for the cursor-div – so that the hover event goes through. (you also forgot an e in “mouseenter“
Working example:
JavaScript
1
21
21
1
const cursor = document.querySelector('.custom-cursor');
2
3
// Custom cursor follow the default cursor
4
document.addEventListener('mousemove', e => {
5
cursor.setAttribute('style', 'top: '+(e.pageY - 10)+'px; left: ' +(e.pageX - 10)+'px;')
6
});
7
8
const links = document.querySelectorAll('a');
9
10
// Custom cursor change style on hover links
11
for(let x of links) {
12
13
x.addEventListener('mouseenter', () => {
14
cursor.classList.add('hover');
15
});
16
17
x.addEventListener('mouseleave', () => {
18
cursor.classList.remove('hover');
19
});
20
21
}
JavaScript
1
17
17
1
.custom-cursor {
2
width: 20px;
3
height: 20px;
4
border: 2px solid orange;
5
position: absolute;
6
border-radius: 50%;
7
transition-duration: 100ms;
8
transition-timing-function: ease-out;
9
box-shadow: 0 0 0 2px black;
10
pointer-events: none;
11
}
12
13
.custom-cursor.hover {
14
width: 40px;
15
height: 40px;
16
background: rgba(#FFCC00,.5);
17
}
JavaScript
1
4
1
<div class="custom-cursor"></div>
2
<a href="#">troet</a>
3
<a href="#">quak</a>
4
<a href="#">miau</a>