How to make your own custom cursor for your website

by Nadal Vicens Date: 08-06-2022 cursor mouse custom design web design javascript css


When I started browsing different and original websites to learn from them, one of the first things that caught my attention was that some of them had their own cursors, so I investigated how to incorporate it into my website.

Today I bring you a simple version of a custom cursor so that you can use it as a base and so that you can begin to play until you get to have the cursor you want.

The html is very simple, it is a simple div with the class you want to put in my case will be "cursor" and I have added a list to have something and see how the cursor interacts.

<body>
    <div class="cursor"></div>
    <ul>
        <li>Enlace 1</li>
        <li>Enlace 2</li>
        <li>Enlace 3</li>
        <li>Enlace 4</li>
    </ul>
</body>

After having the html we have to add the css of our cursor, it is a simple black circle with absolute position, I have added a transition to him for when we add interactions to him it is seen with a small short transition.

.cursor {
            width: 2rem;
            height: 2rem;
            border: solid 2px #000000;
            border-radius: 50%;
            position: absolute;
            transform: translate(-50%, -50%);
            pointer-events: none;
            transition: all ease 0.1s;
        }

Now that we have the css we go with the javascript so that we can use that circle like cursor, we make a variable with the div of the cursor and I have made another with the li that I will use them as if they were links.

To the cursor we put a function to move that calculates the horizontal and vertical position of the cursor and applies it to the div that we have created positioning it, that is why it is important that had absolute position, if you look at the css also had a translate, what makes this translate is that the position of the cursor is in the center of the div that we have created, if we did not put it would be at the top left, you can try it if you want and if you do not want it in the center position it as you want.

let raton = document.querySelector(".cursor");
let enlaces = document.querySelectorAll("li");
window.addEventListener("mousemove", moveCursor);
function moveCursor(e) {
    raton.style.top = e.pageY + "px";
    raton.style.left = e.pageX + "px";
}

Once we check that our cursor is working and is right where the default cursor is, we would have to remove the cursor that is by default, it is very simple, simply remove the cursor throughout the web, it's that easy.

*{
    cursor: none;
}

  

And finally we are going to add the effect of click and hover. I have made a class in css of how I want the cursor to be when I click or pass over an element that can be clicked.

.cursor-click {
    width: 1rem;
    height: 1rem;
    border: solid 3px #27e6bc;
    background-color: hsla(167, 79%, 53%, 0.4);
}
.cursor-hover {
    border: solid 3px #27e6bc;
}

I have also applied css to the li and a hover to see how everything interacts as a whole.

li {
    font-size: 3rem;
    list-style: none;
    margin: 3rem;
    text-align: center;
}
li:hover {
    color: rgb(187, 255, 0);
}

First let's see the click effect, just add two simple functions, adding the click class on click and removing it when the mouse is released.

window.addEventListener("mousedown", () => {
    raton.classList.add("cursor-click");
});
window.addEventListener("mouseup", () => {
    raton.classList.remove("cursor-click");
});

Now we will add the hover to the li elements, you can add it to whatever you want, but if there are going to be many different elements I recommend you to make a class and add it to all the elements that you want to interact in this way with the cursor.

Simply we make a for each with all the elements and we add the 2 functions as before, one of over to add the class when being on top of the element, and another of out to remove that class when leaving that element.

enlaces.forEach(function (element) {
    element.addEventListener("mouseover", function () {
        raton.classList.add("cursor-hover");
    });
    element.addEventListener("mouseout", function () {
        raton.classList.remove("cursor-hover");
    });
});

And we would already have the basics for our cursor to work, now it's time to try to make it more personal or to your style and try different things like adding a svg, put elements inside the cursor div and apply some filter, but from here it's up to your imagination.

I give you a link with the example and the code Here so it will be easier for you to start trying to customize your cursor.

 
by Nadal Vicens Date: 08-06-2022 cursor mouse custom design web design javascript css hits : 1265  
 
Nadal Vicens

Nadal Vicens

Desde niño apasionado de los videojuegos, gracias a ello me ha interesado la tecnología, y he acabado dentro del mundo de la programación, donde puedo estar horas sin que me de cuenta. Interesado en aprender tanto de la parte de tecnología como de diseño. No podría vivir sin videojuegos, café y pizza.

 
 
 

Related Posts

How to use the endsWith method in JavaScript

In this short tutorial, we are going to see what the endsWith method, introduced in JavaScript ES6, is and how it is used with strings in JavaScript. The endsWith method is…

What are javascript symbols and how can they help you?

Symbols are a new primitive value introduced by ES6. Their purpose is to provide us unique identifiers. In this article, we tell you how they work, in which way they…

Callbacks in JavaScript

Callback functions are the same old JavaScript functions. They have no special syntax, as they are simply functions that are passed as an argument to another function. The function that receives…

How to create PDF with JavaScript and jsPDF

Creating dynamic PDF files directly in the browser is possible thanks to the jsPDF JavaScript library. In the last part of this article we have prepared a practical tutorial where I…

Open source web design tools alternatives

There are many prototyping tools, user interface design tools or vector graphics applications. But most of them are paid or closed source. So here I will show you several open…

Node.js and npm: introductory tutorial

In this tutorial we will see how to install and use both Node.js and the npm package manager. In addition, we will also create a small sample application. If you…

How to connect to MySQL with Node.js

Let's see how you can connect to a MySQL database using Node.js, the popular JavaScript runtime environment. Before we start, it is important to note that you must have Node.js installed…

JavaScript Programming Styles: Best Practices

When programming with JavaScript there are certain conventions that you should apply, especially when working in a team environment. In fact, it is common to have meetings to discuss standards…

Difference between arrow and normal functions in JavaScript

In this tutorial we are going to see how arrow functions differ from normal JavaScript functions. We will also see when you should use one and when you should use…

JavaScript Arrow functions: What they are and how to use them

In this article we are going to see what they are and how to use JavaScript Arrow Functions, a new feature introduced with the ES6 standard (ECMAScript 6). What are Arrow…

How to insert an element into an array with JavaScript

In this brief tutorial you will learn how to insert one or more elements into an array with JavaScript. For this we will use the splice function. The splice function will not…

What is the difference between primitives types and objects in JavaScript?

In this short tutorial we are going to look at the differences between primitive types and objects in JavaScript. To start with, we're going to look at what primitive types…

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration