2.0.1 ā€¢ Published 10 months ago

@phucbm/cursorjs v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

šŸ¦„ Cursor.js

Create custom mouse cursor with pure vanilla JS (4kb) and CSS (optional) with no dependency.

npm-version total-download Made in Vietnam jsdelivr license Netlify Status

Key features:

  • Cursor moving with easing (no dependencies)
  • Extremely light-weight (4kb)
  • One cursor - unlimited styles on hover

Demo šŸ‘‰ https://cursorjs.netlify.app

Getting started

Installation

NPM Package

npm i @phucbm/cursorjs

Import

import "@phucbm/cursorjs";

Download

šŸ‘‰ Self hosted - Download the latest release

<script src="./cursorjs.min.js"></script>

šŸ‘‰ CDN Hosted - jsDelivr

<!-- JS (10KB) -->
<script src="https://cdn.jsdelivr.net/gh/phucbm/cursorjs@latest/dist/cursorjs.min.js"></script>

Initialize

// with default style
Cursorjs.create();

// with more options
Cursorjs.create({
    id: 'my-cursor',
    innerHTML: '<i class="icon-cursor"></i>',
    hover: [
        {
            selectors: '.items',
            className: 'my-style-class'
        },
    ]
});

Docs

Cursor options

NameTypeDefaultDescription
idstring"css-cursor-<uniqueID>"Set ID to get instance
speedfloat0.2Cursor easing speed, the smaller, the slower
containerDOM elementdocument.bodyWhere to append cursor HTML
classNamestring""Class for cursor
innerHTMLstring""Inner HTML for cursor
classInViewportstring""Class when cursor is in viewport
matchMediastring"(hover:hover)"Only init if match this media
hoverPrefixClassstring""Prefix for hover class.
hoverstring[]Actions when hover on specific elements. See Hover object below.
wrapperCSSCSS object{...}Default style for cursor wrapper (*)
cursorCSSCSS object{...}Default style for cursor (*)

(*) default CSS

const options = {
    wrapperCSS: {
        pointerEvents: 'none',
        zIndex: '9999',
        position: 'fixed',
        top: 0,
        left: 0,
    },
    cursorCSS: {
        boxShadow: '0 0 0 2px rgba(0, 0, 0, .3)',
        width: '40px',
        height: '40px',
        borderRadius: '50%',
        transition: 'all .3s ease',
        position: 'absolute',
        transform: 'translate(-50%,-50%)'
    }
}

Hover object

NameTypeDefaultDescription
selectorsstring""CSS selector, multiple elements is supported
classNamestring""Add this class to cursor when hover on selectors
cursorstring"none"CSS cursor when hover on selectors
// sample hover array
const options = {
    hover: [
        {
            selectors: '.item-a, .item-b',
            className: 'is-hover-on-items',
            cursor: 'pointer',
        },
        {
            selectors: '.item-c',
            className: 'is-hover-on-item-c',
            cursor: 'none',
        }
    ]
}

Methods

const cursor = Cursorjs.get('my-cursor');

// remove cursor from DOM
cursor.destroy();

// check new hover selectors, useful when new items are loaded via AJAX
cursor.refresh();

Deployment

# Run dev server
npm run dev

# Generate UMD and module version
npm run prod

# Build production site
npm run build

# Generate UMD and module version then publish NPM package
npm run publish

License

MIT License

Copyright (c) 2023 @phucbm

2.0.1

10 months ago

2.0.0

1 year ago