A powerful javascript library to create amazing and smooth effects for the mouse cursor on your website.

Overview

Cuberto Mouse Follower

NPM Version Licence Bundle file size Bundle file size (gzip) NPM Downloads GitHub Workflow Status

A powerful javascript library to create amazing and smooth effects for the mouse cursor on your website.

demo

Dependencies

GSAP v3 (https://greensock.com/gsap/)

Installation

npm install gsap --save
npm install mouse-follower --save

Quick start

Import cursor styles from /src/scss/index.scss into your main SCSS file:

@import "cursor";

Mouse Follower requires GSAP library to work.

Import GSAP, import Mouse Follower and initialize it in JS:

import MouseFollower from "mouse-follower";
import gsap from "gsap";

MouseFollower.registerGSAP(gsap);

const cursor = new MouseFollower();

Options

You can configure cursor follower via options:

const cursor = new MouseFollower({
    container: ".mf-container",
    speed: 0.3
});

The following options with defaults are available:

const cursor = new MouseFollower({
    el: null,
    container: document.body,
    className: "mf-cursor",
    innerClassName: "mf-cursor-inner",
    textClassName: "mf-cursor-text",
    mediaClassName: "mf-cursor-media",
    mediaBoxClassName: "mf-cursor-media-box",
    iconSvgClassName: "mf-svgsprite",
    iconSvgStatePrefix: "-",
    iconSvgSrc: "",
    dataAttr: "cursor",
    hiddenState: "-hidden",
    textState: "-text",
    iconState: "-icon",
    activeState: "-active",
    mediaState: "-media",
    stateDetection: {
        "-pointer": "a,button",
        "-hidden": "iframe"
    },
    speed: 0.55,
    ease: "expo.out",
    overwrite: true,
    skewing: 0,
    skewingText: 2,
    skewingIcon: 2,
    skewingMedia: 2,
    skewingDelta: 0.001,
    skewingDeltaMax: 0.15,
    stickDelta: 0.15,
    showTimeout: 20,
    showOnEnter: true,
    hideOnLeave: true,
    hideTimeout: 300,
    hideMediaTimeout: 300,
    initialPos: [-window.innerWidth, -window.innerHeight],
});
Name Type Description
el string | HTMLElement Existed cursor element. If not specified, the cursor will be created automatically.
container string | HTMLElement Cursor container. Body by default.
className string Cursor root element class name.
innerClassName string Inner element class name.
textClassName string Text element class name.
mediaClassName string Media element class name.
mediaBoxClassName string Media inner element class name.
iconSvgClassName string SVG sprite class name.
iconSvgNamePrefix string SVG sprite class name prefix of icon.
iconSvgSrc string SVG sprite source. If you are not using SVG sprites leave this blank.
dataAttr string | null Name of data attribute for changing cursor state directly in HTML markdown. Uses an event delegation.
hiddenState string Hidden class name state.
textState string Text class name state.
iconState string Icon class name state.
activeState string Active (mousedown) class name state.
mediaState string Media (image/video) class name state.
visible number Show the cursor when mouse enter container. This also means that the cursor will be visible by default.
stateDetection object | null Allow to set predefined states for different elements on page. Uses an event delegation.
speed number Cursor movement speed.
ease string Timing function of cursor movement. See gsap easing.
overwrite boolean Overwrite or remain cursor position when mousemove event happened. See gsap overwrite modes.
skewing number Default "skewing" factor.
skewingText number Skew effect factor in a text state. Set 0 to disable skew in this mode.
skewingIcon number Skew effect factor in a icon state. Set 0 to disable skew in this mode.
skewingMedia number Skew effect factor in a media (image/video) state. Set 0 to disable skew in this mode.
skewingDelta number Skew effect base delta. Set 0 to disable skew in this mode.
skewingDeltaMax number Skew effect max delta. Set 0 to disable skew in this mode.
stickDelta number Stick effect delta.
showTimeout number Delay before show. May be useful for the spawn animation to work properly.
hideOnLeave boolean Hide the cursor when mouse leave container.
hideTimeout number Hiding delay. Should be equal to the CSS hide animation time.
initialPos array Array (X, Y) of initial cursor position.

Advanced usage

Show or hide cursor

These basic methods allow you to show and hide the cursor:

const cursor = new MouseFollower();
const el = document.querySelector('.my-element');

el.addEventListener('mouseenter', () => {
    cursor.hide();
});

el.addEventListener('mouseleave', () => {
    cursor.show();
});

or via data attribute:

<div data-cursor="-hidden">Hover me to hide cursor!</div>

Toggle cursor state

A state is essentially a class that applies to the root element of the cursor. You can change the appearance of the cursor using CSS (see cursor.scss).

To set/unset state use methods:

const cursor = new MouseFollower();
const el = document.querySelector('.my-element');

el.addEventListener('mouseenter', () => {
    cursor.addState('-inverse'); // you can pass multiple states separated by whitespace
});

el.addEventListener('mouseleave', () => {
    cursor.removeState('-inverse');
});

or via data attribute:

<div data-cursor="-inverse">Hover me to inverse cursor!</div>

State detection

You can customize the list of states for all elements on the page:

const cursor = new MouseFollower({
    stateDetection: {
        "-pointer": "a,button",
        "-opaque": ".my-image",
        "-hidden": ".my-input"
    }
});
<a>On this element cursor will be in pointer state</a>
<div class="mf-image">On this element cursor will be in opaque state</div>
<div class="mf-input">On this element cursor will be hidden</div>

Note: State detection feature uses an event delegation. Do not create large amount rules and complex selectors to avoid performance problems. It is recommended to disable this in projects with a large number of nested DOM elements. This also applies to binding via data attribute.

To fully disable event delegation:

const cursor = new MouseFollower({
    stateDetection: false,
    dataAttr: false
});

Text mode

To display text in the cursor use this method:

const cursor = new MouseFollower();
const el = document.querySelector('.my-element');

el.addEventListener('mouseenter', () => {
    cursor.setText('Hello!');
});

el.addEventListener('mouseleave', () => {
    cursor.removeText();
});

or via data attribute:

<div data-cursor-text="Hello!">Hover me!</div>

Icon mode

If you use SVG spritesheet in your project and want to display them in the cursor, then you can use this method. In this case, you need to specify the path to the SVG sprite in the options and set class names.

const cursor = new MouseFollower({
    iconSvgSrc: "/assets/img/sprites/svgsprites.svg",
    iconSvgClassName: "my-spritesheet",
    iconSvgNamePrefix: "-",
});
const el = document.querySelector('.my-element');

el.addEventListener('mouseenter', () => {
    cursor.setIcon('arrow-left');
});

el.addEventListener('mouseleave', () => {
    cursor.removeIcon();
});

or via data attribute:

<div data-cursor-icon="arrow-left">Hover me!</div>

Image mode

This method allows you to show any picture in the cursor:

const cursor = new MouseFollower();
const el = document.querySelector('.my-element');

el.addEventListener('mouseenter', () => {
    cursor.setImg('/img/example.png')
});

el.addEventListener('mouseleave', () => {
    cursor.removeImg()
});

or via data attribute:

<div data-cursor-img="/img/example.png">Hover me to show image!</div>

Video mode

You can also play videos:

const cursor = new MouseFollower();
const el = document.querySelector('.my-element');

el.addEventListener('mouseenter', () => {
    cursor.setVideo('/video/example.mp4');
});

el.addEventListener('mouseleave', () => {
    cursor.removeVideo();
});

or via data attribute:

<div data-cursor-video="/video/example.mp4">Hover me to show movie!</div>

Sticky effect

This method allows you to attach the cursor to an element with a magnet effect. This only works correctly with fixed elements on the page.

const cursor = new MouseFollower();
const box = document.querySelector('.my-fixed-box');
const el = document.querySelector('.my-fixed-element');

box.addEventListener('mouseenter', () => {
    cursor.setStick(el);
});

box.addEventListener('mouseleave', () => {
    cursor.removeStick();
});

or via data attribute:

<div data-cursor-stick>Hover me to stick cursor!</div>

You can also pass element selector to data attribute:

<div data-cursor-stick="#stick-me">Hover <div id="stick-me">me</div> to stick cursor!</div>

Skewing effect

The skew effect is the distortion of the cursor when moving. It looks good with round cursors.

const cursor = new MouseFollower();
const el = document.querySelector('.my-element');

el.addEventListener('mouseenter', () => {
    cursor.setSkewing(3);
});

el.addEventListener('mouseleave', () => {
    cursor.removeSkewing();
});

Hidden cursor

In this example, the cursor is initialized hidden by default and only appears on the desired element.

const cursor = new MouseFollower({
    visible: false
});
const el = document.querySelector('.my-element');

el.addEventListener('mouseenter', () => {
    cursor.show();
    cursor.setText('Surprise!');
});

el.addEventListener('mouseleave', () => {
    cursor.removeText();
    cursor.hide();
});

or via data attribute:

<div data-cursor-show data-cursor-text="Surprise!">Hover me to show cursor!</div>

Destroy cursor instance

Destroy the cursor completely and remove all event listeners.

const cursor = new MouseFollower();

cursor.destroy();

Examples of use

  • Cuberto: Leading digital agency.
  • Potion: Video email for top sales professionals.
  • NANA: Digital Magazine and Media Company in Asia.
  • Wickret: 100% digital bank designed for you.
  • Papumba: Educational platform for kids.
  • Safe Security: Cyber Risk Quantification For Enterprises.

License

The MIT License (MIT)

Comments
  • data-cursor-stick centered in wide elements

    data-cursor-stick centered in wide elements

    Sticky mode failed to center properly with an item aspect ratio greater than 1/1

    https://user-images.githubusercontent.com/48869708/172047314-c41e3705-2d2a-410c-8dd0-bf556dfc6487.mp4

    opened by drementer 2
  • StateDetection property TypeError

    StateDetection property TypeError

    Hello, I encountered an issue with your library; the code I used is the most basic one to run an example.

    import MouseFollower from "mouse-follower";
    import gsap from "gsap";
    
    MouseFollower.registerGSAP(gsap);
    const cursor = new MouseFollower();
    

    Versions with this problem: 1.0.4 to 1.0.6 1.0.3 and below work fine.

    Error details

    image

    image

    Environment details

    Edge 101.0.1210.53 React 18.1.0 Gsap 3.10.3 (3.10.4 doesn't solve this) Sass 1.52.1

    opened by Giuseppetm 2
  • Media cursor issue on RTL websites

    Media cursor issue on RTL websites

    When the website direction is from RTL data-cursor-img The cursor doesn't change to an image, Rather, the image comes next to the cursor, and the cursor remains the same as it.

    ✅I added the direction[LTR] to fix that -> css

    .mf-cursor { direction: ltr; }

    Thanks ❣️

    bug 
    opened by HasanSibakhi 2
  • State detection documentation example error

    State detection documentation example error

    Hello,

    here you can find a little error in the readme about state detection. The classes name should be my-image and my-input (not mf-image and mf-input).

    image

    opened by Giuseppetm 1
  • Error in readme

    Error in readme

    First of all, thank you!

    Not sure if this is a mistake in the readme file: <div data-cursor-stick-stick="#stick-me">Hover <div id="stick-me">me</div> to stick cursor!</div>

    Looks like there is one to many "-stick".

    opened by agostinijoerg 1
  • How to properly import in next app

    How to properly import in next app

    I have done 'npm install mouse-follower --save' on my next app. How do I correctly import it. I managed to import somehow in _app.tsx but every time page refresh the line 'const cursor = new MouseFollower();' throws an server error and crasheh the web page. But if I commented it and uncomment it then it works fine. But after this if the page refresh it again shows the same error.

    question 
    opened by sagarkhadka 15
  • Change 'mousemove' to 'pointermove'

    Change 'mousemove' to 'pointermove'

    I've had some lags when working with sliders, especially when I'm dragging a slider. The lags disappeared when I changed 'mousemove' to 'pointermove'.

    There were lags when working with swiper and flickity sliders. I haven't tested it on the other sliders.

    https://user-images.githubusercontent.com/25268319/188215522-1e6f42ae-f1d8-4bd5-a619-db67f14bc6d8.mov

    https://user-images.githubusercontent.com/25268319/188215531-5e75defd-a239-4926-9300-e0eb1913d863.mov

    opened by nurpais 2
  • Two state at the same time.

    Two state at the same time.

    is it possible to enable two states at the same time?

    I want to set a img and text to the cursor, the picture and text are displayed, but without the text background.

    I apologize if I missed something in the documentation. Thank you!

    image

    enhancement question 
    opened by nurpais 2
  • Using with smooth scrolling libraries

    Using with smooth scrolling libraries

    Hello! it's an amazing library.

    I have some issues when working with safari. If I hover the cursor over an image, text appears or when I hover over links or buttons, the cursor decreases. The problem is that if I scroll down, the cursor remains in the same state. Everything works fine in google chrome. Maybe I missed something.

    question 
    opened by nurpais 5
  • Page transitions with Barba JS

    Page transitions with Barba JS

    When i click on a link to the next page the cursor doesn't disappear.

    This hook code doesn't remove the cursor on page transition. barba.hooks.beforeEnter(()

    Kindly advise how to fix this, if possible provide a fix here to enable other users see it.


    Also you should have a Slack or discord community where users like me ask questions and get help from the community.

    question 
    opened by Louboutin1017 4
Releases(v1.1.2)
Owner
Cuberto
Leading digital agency with solid design and development expertise.
Cuberto
Replaces native cursor with custom animated cursor.

Animated Cursor A pure JS library to replace native cursor with a custom animated cursor. Check out the Demo Contents ?? Features ?? Quickstart ?? Opt

Stephen Scaff 7 Jul 18, 2022
A JavaScript library for adding ripple effects to HTML elements based on mouse events.

About project Ripplejs is an open source javascript library created for the purpose of adding ripple effects to html elements based on mouse events. L

Udezue Oluomachi Chimaobi 7 May 10, 2022
Smooth scrolling effect (while using mouse wheel). No jQuery or other unnecessary stuff needed.

scrooth Smooth scrolling effect (while using mouse wheel). No jQuery or other unnecessary stuff needed. Why? I needed that, and I was unable to find p

Rafał Spiżewski 20 Aug 29, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Jspreadsheet is a lightweight vanilla javascript plugin to create amazing web-based interactive tables and spreadsheets compatible with other spreadsheet software.

Jspreadsheet CE v4: The JavaScript spreadsheet Jexcel CE has been renamed to Jspreadsheet CE News Important: Please import jspreadsheet.css (jexcel.cs

null 6.2k Dec 19, 2022
Create a new challenge for jshero.platzi.com, share your experience creating amazing challenges.

JSHero - jshero.platzi.com ?? Create a new challenge for jshero.platzi.com, share your experience creating amazing challenges. Create a new challenge:

Comunidad Platzi 28 Oct 19, 2022
Create a new challenge for jshero.platzi.com, share your experience creating amazing challenges.

JSHero - jshero.platzi.com ?? Create a new challenge for jshero.platzi.com, share your experience creating amazing challenges. Create a new challenge:

PlatziLabs 20 Apr 27, 2022
Create amazing pixel art murals. 🐸

Pixelate Create amazing pixel art murals with sticky notes. Pixelate is a image editor that shows assembly guides to put art with sticky notes on your

Google 19 Dec 11, 2022
🎨 Beautify your github profile with this amazing tool, creating the readme your way in a simple and fast way 🚀 The best profile readme generator you will find ⚡

Demo Profile Readme Generator The best profile readme generator you will find! About | Technologies | Requirements | Starting | Contributing ?? About

Mauro de Souza 476 Jan 1, 2023
TS & JS Library for adaptive precision cursor for the web. Releases will come out soon! Meanwhile, check out the demo site:

Haha, cool cursor go brrrr... Table of Content What is this? Installation & Setup Installation Setup Usage Cursor controls Element settings Known issu

LemonOrange 10 Nov 24, 2022
A collection of preloaded and essential files that makes the website more attractive, smooth and user friendly

Web-Docs A collection of preloaded and essential files that makes the website more attractive, smooth and user friendly How to use: git clone https://

MAINAK CHAUDHURI 23 Dec 17, 2022
Contribute on this repository with VALID PRs to HACKTOBERFEST-2022 and earn amazing swags!

LINK- https://rbshop.netlify.app/ Hacktoberfest2022 You can make folders of programming languages and also you can contribute in the repo's below. Fee

Ranodeep Banerjee 17 Oct 25, 2022
Amazing-Js-Projects

Amazing-Js-Projects This project is a part of the following Open Source Program Levels & Points Level Points Level 0 5 Level 1 10 Level 2 20 Level 3 4

Arpit Jain 124 Jan 3, 2023
An Amazing SlashCommands Handler (With Sharding & Mongo) Made by discord.gg/azury

SlashCommands Handler by The Azury Team If this Git-Repo gets "40" Stars ⭐ i'll add some more Commands! ??️ FEATURES: 1. SlashCommands Support 2. Cont

Masterious 24 Dec 2, 2022
A collection of amazing plugins made by IITC community.

IITC Community plugins A collection of amazing plugins made by community. Note: You must install IITC-CE on your desktop or mobile device to use IITC

Ingress Intel Total Conversion — Community Edition 16 Dec 29, 2022
MagicSniffer is a amazing tool could help you decrypt GI traffic by MAGIC of WindSeedClientNotify

MagicSniffer We have posted an article about this on sdl.moe: 原神 2.8 KCP 验证密钥交互流程解析与流量解密 As everyone knows, RSA is the most secure way to encrypt data

Sorapointa 49 Dec 29, 2022
a JavaScript library that allows you to make a mouse interaction animation easily.

Cotton.JS is a JavaScript library that allows you to make a mouse interaction animation easily. Getting Started Download npm install cottonjs Manual

Wilson Wu 20 Dec 27, 2022