banner



How To Make Your Own Animated Cursor

The custom cursor

If you curl through the Awwwards website, you'll see them very ofttimes. Animated cursors. Sometimes its a cirkel below your cursor, sometimes it completely replaces your cursor. Some cursors animate on click, some breathing on hover. When idea out right, it can add a lot to your make. But… How do y'all build one?

There are a few ways how you lot tin can build blithe cursors. By using a sail element, or simply by using div'south. To continue this brusque tutorial as easy as possible, nosotros'll be using div's.

Image of wavy text

Let's go started

Start we'll be adding one or multiple div elements (in the example below I added ane div. Information technology's called ".cursorWrapper"). In CSS yous'll give this element a fixed position, a top and a left with the value "0" and a transform that takes 2 css-variables. That volition later on be the "10" and a "y" coordinates of your mouse.

Your CSS should now look like this.

            

1 :root {

2 --posX : 0 px

three --posY : 0 px

4 }

5

half-dozen .cursorWrapper {

7

8 transform : translate3d ( var ( --posX ) , var ( --posY ) , 0 ) ;

9 position : stock-still ;

ten top : 0 ;

11 left : 0 ;

12 arrow-events : none ;

13 }

Now yous have your base. Y'all still don't see annihilation. The bodily visuals, nosotros put in a ::before pseudo-element. We're doing this, because if nosotros want to animate the scale asynchronously from the position we'd rather have them in split selector.

Now we want to create the cursor itself. Allow'due south create a earlier pseudo-element. This chemical element wil be the visible cursor. Don't forget that ::before needs a "content" belongings and "position: absolute".

In terms of styling you can exercise what you desire. Make sure you add a width and height. And likewise disable pointer-events in your CSS. We practise this, so you lot're able to click through the custom cursor. We add this code to our CSS file:

            

1 .cursorWrapper ::earlier {

2 position : accented ;

3 width : 50 px ;

4 peak : fifty px ;

5 arrow-events : none ;

6

7 groundwork-color : purple ;

eight border-radius : 25 px ;

ix }

Add any other styling you want. You tin for case employ clip-path to brand your cursor get different shapes.

Adding the mouse movement

So right at present, we have all the styling of the cursor, only it still doesn't piece of work. So we'll be adding a layer of JavaScript right at present. We'll start bij adding a constant where we store a reference to the root.

Then we'll exist calculation the event listener. We'll exist listening to the mouse mouse on the torso. In one case we've done that, we'll be adding a function. In my case it'southward called "applyCursorPos". We need the clientX and the clientY of the event. Since this function volition be chosen direct from the issue listener, the event parameter is passed automagicly. We can destructure that. In the parameter of the "applyCursorPos" and use it in the function.

Since the size of our cursor is 50px, nosotros demand to decrease 25px of our cursors position, so the centre of our custom cursor is really the middel. You can do this in JavaScript merely you can also do this in CSS by using a calc part.

By using the setProperty function on the style of root, we'll be able to change the value of the css variable.

            

ane const root = document . querySelector ( ':root' )

2

3 const applyCursorPos = ( { clientX , clientY } ) => {

4 const x = ` ${ clientX - 25 } px `

v const y = ` ${ clientY - 25 } px `

6

vii root . style . setProperty ( "--posX" , x ) ;

8 root . manner . setProperty ( "--posY" , y ) ;

9 }

10

eleven document . body . addEventListener ( "mousemove" , applyCursorPos )

If done correctly, your custom cursor should now follow your mouse motility. You can at present decide if you want your cursor to lay above all the elements and use mix-blend-manner to change the colors of the elements below it, or you lot tin can change the z-index so the custom cursor appears beneath the elements. Take in business relationship that your cursor will also appear below images, which can exist abrasive. A quick fix, is to give all the images an fifty-fifty lower z-index than the custom cursor.

There's a lot more yous tin can do with the custom cursor, for instance, irresolute the color or size when hovering over sure elements. I've done this with buttons, anchors and inputs.

            

ane cursorHoverState = ( e , { hovering } ) => {

2

3

iv const rect = due east . target . getBoundingClientRect ( ) ;

5

six root . style . setProperty ( "--posX" , rect . x + ( rect . width / two ) - 25 + "px" ) ;

vii root . style . setProperty ( "--posY" , rect . y + ( rect . tiptop / ii ) - 25 + "px" ) ;

8

nine root . style . setProperty ( "--color" , hovering ? "orangish" : color ) ;

10 }

11

12 document . querySelectorAll ( "button, a, input, select" ) . forEach ( btn => {

13 btn . addEventListener ( "mouseover" , ( e ) => { cursorHoverState ( e , { hovering : true } ) } )

14 btn . addEventListener ( "mouseout" , ( e ) => { cursorHoverState ( eastward , { hovering : false } ) } )

15 } ) ;

In the cursorHoverState you can practice all kinds of things, you can modify the clip-path of your custom cursor, and such.

Upgrading

There's much more you tin can do. I've added even more than cirkels to my custom cursor and gave them all a different size, opacity and transition filibuster (on mouse move). Please leave a comment about what you remember of the custom cursor and a codepen of your ain, so I can cheque information technology out.

Source: https://webanimation.blog/blog/how-to-create-a-custom-cursor-without-using-canvas/

Posted by: ellingtonditer1959.blogspot.com

0 Response to "How To Make Your Own Animated Cursor"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel