Create a Customized Mouse Cursor with CSS or JavaScript

Personalizing a web site is without doubt one of the extra enjoyable points of net improvement. On this tutorial, we’ll take a look at two strategies so as to add a {custom} mouse cursor to your web site.
Again within the early levels of net improvement, web sites ran rampant with all types of wacky options: galaxy-themed backgrounds, colourful fonts to the purpose of illegibility, and marquees galore.
Supply: cameronsworld.web
Now we’ve kind of calmed down with regards to net design however that doesn’t imply we are able to’t have enjoyable options on our web sites anymore. On this tutorial, we’ll take a look at add a private contact to any web site through the use of a {custom} cursor, firstly utilizing a CSS-only method, then a extra interactive JavaScript technique.
1. Including a Customized Cursor with CSS
On this demo, we’ve carried out two totally different cursors on a web page utilizing solely CSS. Hover over the pen to see what occurs:
The CSS property cursor permits us to alter our cursor to built-in key phrases and it additionally permits us set a picture because the cursor.
Our CSS code seems to be one thing like this:
1 | major { |
2 | cursor: url(“information:picture/svg+xml,%3Csvg xmlns=”https://www.w3.org/2000/svg” top=”16″ width=”16″ type=”fill-rule:evenodd;text-rendering:geometricPrecision;image-rendering:optimizeQuality;clip-rule:evenodd;shape-rendering:geometricPrecision” xml:area=”protect” viewBox=’0 0 7.5 7.5’%3Epercent3Cpath d=’M0 3.8a3.7 3.7 0 1 1 7.5 0 3.7 3.7 0 0 1-7.5 0zm.5 0a3.3 3.3 0 1 0 6.6 0 3.3 3.3 0 0 0-6.6 0zm2.9 0c0 .2.2.3.4.3a.4.4 0 1 0-.4-.3z’ type=”fill:currentColor;stroke:currentColor;stroke-width:.0419595″/%3Epercent3C/svgpercent3E”) 8 8, pointer; |
3 | } |
4 | |
5 | .hover-container { |
6 | cursor: url(https://cur.cursors-4u.web/nature/nat-11/nat1021.cur), default; |
7 | } |
We use the next values to type our cursor:
- url(): The url perform is used to set the picture file for a cursor so long as it’s in an applicable format and a most of 128px by 128px. In our major aspect, we set the cursor as an .svg whereas we use a .cur file in our hover container aspect.
- Coordinates: The x and y coordinate values are positioned after the url perform to specify which level the cursor ought to begin from. By default, cursor photos are positioned within the high left nook of the place the mouse cursor could be. In our major aspect, we set the x and y coordinates to eight 8 to make sure our circle cursor is on the mid-point of the place our mouse is pointing.
- Fallback: The fallback worth is the final key phrase on the finish of our cursor property. The fallback is about to one of many built-in cursor key phrase values and is used if the picture within the url can’t be loaded.
And that’s all there’s to setting a {custom} cursor with CSS!
2. Including a Customized Cursor with JavaScript
If we wish our cursor to have extra interactivity with our webpage, we are able to set it utilizing JavaScript. Hover over the pen to see how the cursor modifications:
To do that, first we’ll want an HTML aspect to behave as our interactive cursor.
1 | id=“custom-cursor”> |
2 |
Subsequent, we type our {custom} cursor aspect CSS. We will move in any picture or styling property to the cursor aspect to make it look precisely like we wish. On this tutorial, we’ll type a easy circle and us the ::after pseudo aspect to kind a dot within the center. Right here’s the CSS styling:
1 | #custom-cursor { |
2 | width: 2px; |
3 | top: 2px; |
4 | border-radius: 50%; |
5 | background-color: white; |
6 | place: fastened; |
7 | pointer-events: none; |
8 | high: 0; |
9 | } |
10 | |
11 | #custom-cursor::after { |
12 | content material: “”; |
13 | border-radius: 50%; |
14 | place: absolute; |
15 | high: -8px; |
16 | left: -8px; |
17 | border: 1px strong white; |
18 | width: 16px; |
19 | top: 16px; |
20 | } |
It’s essential for our {custom} cursor to have the place: fastened and pointer-events: none properties. That is to make sure that the cursor is all the time positioned on the web page by mouse actions and that we’re not in a position to work together with the cursor aspect.
A Splash of Shade
We will add some colour enjoyable to our cursor through the use of the mix-blend-mode property. This can give the cursor an inverted colour impact primarily based on the background it’s hovering over.
1 | #custom-cursor { |
2 | width: 2px; |
3 | top: 2px; |
4 | border-radius: 50%; |
5 | background-color: white; |
6 | place: fastened; |
7 | high: 0; |
8 | mix-blend-mode: distinction; |
9 | } |
That is what we have now to this point:
Combine-blend-mode on the cursor
Cover the Authentic Cursor
Subsequent we wish to disguise our common cursor and solely present the {custom} cursor aspect when the web page is being hovered:
1 | physique { |
2 | cursor: none; |
3 | } |
4 | |
5 | physique:hover #custom-cursor { |
6 | opacity: 1; |
7 | } |
8 | |
9 | #custom-cursor { |
10 | width: 2px; |
11 | top: 2px; |
12 | border-radius: 50%; |
13 | background-color: white; |
14 | place: fastened; |
15 | high: 0; |
16 | mix-blend-mode: distinction; |
17 | opacity: 0; |
18 | } |
Including the JavaScript Magic
Now we’ve gotten the styling out of the way in which, let’s set the cursor performance with JavaScript.
Our performance will deal with positioning the {custom} cursor primarily based on the mouse motion and in addition the cursor interplay with components on the web page.
The code for updating the cursor place is:
1 | const customCursor = doc.getElementById(‘custom-cursor‘); |
2 | |
3 | const updateCursorPosition = (occasion) => { |
4 | customCursor.type.high = `${occasion.clientY}px`; |
5 | customCursor.type.left = `${occasion.clientX}px`; |
6 | } |
7 | |
8 | window.addEventListener(‘mousemove‘, (occasion) => { |
9 | updateCursorPosition(occasion) |
10 | }) |
We use the clientX and clientY values to set the cursor coordinates each time the mouse is moved.
We will additionally replace the cursor styling each time it interacts with a component. We’ll add a zoom class to the cursor aspect when it hovers over the hover-container aspect.
Let’s replace our CSS to incorporate styling for the zoom class:
1 | #custom-cursor { |
2 | width: 2px; |
3 | top: 2px; |
4 | border-radius: 50%; |
5 | background-color: white; |
6 | place: fastened; |
7 | high: 0; |
8 | opacity: 0; |
9 | pointer-events: none; |
10 | mix-blend-mode: distinction; |
11 | transition: rework 500ms; |
12 | } |
13 | |
14 | #custom-cursor.zoom { |
15 | rework: scale(3); |
16 | } |
We will use the .matches() perform to focus on when the hover-container is being hovered (and this fashion we received’t have to connect one other occasion listener to the aspect).
Right here’s what the ultimate JavaScript code seems to be like:
1 | const customCursor = doc.getElementById(‘custom-cursor‘); |
2 | const hoverContainer = doc.querySelector(‘.hover-container‘); |
3 | |
4 | const updateCursorPosition = (occasion) => { |
5 | customCursor.type.high = `${occasion.clientY}px`; |
6 | customCursor.type.left = `${occasion.clientX}px`; |
7 | } |
8 | |
9 | window.addEventListener(‘mousemove‘, (occasion) => { |
10 | updateCursorPosition(occasion) |
11 | |
12 | if (hoverContainer.matches(‘:hover‘)) { |
13 | customCursor.classList.add(‘zoom‘) |
14 | } else { |
15 | customCursor.classList.take away(‘zoom‘) |
16 | } |
17 | }) |
Conclusion
And with that, along with our easy CSS cursor, we’ve constructed a light-weight model of an interactive {custom} cursor, utilizing solely vanilla JavaScript.