A jQuery plugin for using FontAwesome icons as custom CSS cursors. Works in Chrome and FireFox.
Try hovering over this!
Now also supports using an alternative icon font!
In your terminal:
npm install jquery-awesome-cursor
In your web page:
<link rel="stylesheet" href="node_modules/font-awesome/css/font-awesome.min.css">
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="node_modules/jquery-awesome-cursor/dist/jquery.awesome-cursor.min.js"></script></pre>
If you plan on using jquery-awesome-cursor
with a different icon font, you can skip the FontAwesome dependency:
npm install --no-optional jquery-awesome-cursor
In your terminal:
bower install jquery-awesome-cursor
In your web page:
<link rel="stylesheet" href="bower_components/fontawesome/css/font-awesome.min.css">
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/jquery-awesome-cursor/dist/jquery.awesome-cursor.min.js"></script>
IMPORTANT: As of v0.3.0
, FontAwesome is considered an optional dependency. Therefore, you must install it yourself!
bower install font-awesome
The download links below are for the latest versions of the code, which may be unstable.
To view the latest stable release, and previous releases, please visit https://github.com/jwarby/jquery-awesome-cursor/releases .
Download just the script:
jquery.awesome-cursor.min.js (production) jquery.awesome-cursor.js (development)Or download an archive of the latest code:
.zip .tar.gzThen, in your web page:
<link rel="stylesheet" href="/path/to/font-awesome.min.css">
<script src="/path/to/jquery.min.js"></script>
<script src="/path/to/jquery.awesome-cursor.min.js"></script></pre>
You could also use RawGit's CDN:
<link rel="stylesheet" href="path/to/font-awesome.min.css">
<script src="path/to/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/jwarby/jquery-awesome-cursor/master/dist/jquery.awesome-cursor.min.js"></script></pre>
To set a cursor on an element, just call $('<selector>').awesomeCursor('<icon name>');
For example, to use the pencil icon as the <body>
element's cursor, do the following:
$('body').awesomeCursor('pencil');
Cursors also support a number of options, which are detailed in the next section.
Options are set by passing an object as the second parameter to awesomeCursor()
:
$('body').awesomeCursor('pencil', {
/* your options here */
});
You can reset the cursor to it's previous icon using jQuery's css
function:
$('body').css('cursor', '');
The cursor attributes can be configured using the options listed below.
type: String
The first parameter to awesomeCursor
is the icon name.
$('body').awesomeCursor('pencil');
type: String
The color of a cursor can be changed by setting the color
option to any valid CSS color string:
$('body').awesomeCursor('pencil', {
color: 'red'
});
type: Number
The size of the cursor can be changed by setting the size
option.
Note: cursor sizes can only be defined in pixels, as the cursor hotspot CSS syntax only supports pixels
$('body').awesomeCursor('pencil', {
size: 32
});
type: String or Array
The hotspot location of the cursor can be defined using the hotspot
option. This allows you to configure the point that the actual click will emanate from.
The hotspot can be configured in two different ways: using an array containing the [x, y]
coordinates (in pixels) of the hotspot, or by using a descriptive string
.
If you're using an array of coordinates, the values will be clamped between 0 and the size of the cursor minus 1. This is because the browser will default to 0, 0
for coordinates beyond the
boundaries of the cursor.
$('body').awesomeCursor('pencil', {
hotspot: [5, 7]
});
The strings 'bottom', 'top', 'left', 'right' and 'center' are recognised. These values can be combined by space-separating them; some examples are shown below:
$('body').awesomeCursor('pencil', {
hotspot: 'bottom left'
});
type: String
The cursor can be flipped horizontally, vertically, or in both directions by setting the flip
option.
flip
can be one of:
To disable flipping, set flip
to any falsy value (false
, null
, ''
, etc.). All other
values will result in an error being thrown.
$('body').awesomeCursor('pencil', {
flip: 'horizontal'
});
type: Number
The cursor can be rotated any number of degrees using the rotate
option.
$('body').awesomeCursor('pencil', {
rotate: 45
});
type: String
An outline can be applied to a cursor by setting the outline
option to any valid CSS color string.
$('body').awesomeCursor('pencil', {
outline: 'black'
});
The default values for all options are exposed by $.fn.awesomeCursor.defaults
. This means you can easily override default options:
$.fn.awesomeCursor.defaults.color = 'white'; // Default color for all cursors is now white
You may want to use an icon font other than FontAwesome for your cursors - this can be achieved using the font
option (as of v0.1.0).
The font
option is an object, containing the font family
and cssClass
. The example below demonstrates how to use typicons as the icon font:
$('body').awesomeCursor('brush', {
font: {
family: 'typicons',
cssClass: 'typcn typcn-%s'
}
});
family
option is the name of the font icon's family. For FontAwesome, it's 'FontAwesome'; for typicons it's 'typicons'cssClass
option is the format of the replacement font icon's CSS classes, where %s
is the name of the icon passed as the first parameter to awesomeCursor
. You can also
use a function instead of a string - the function receives the icon name as it's only parameter, and is expected to return the CSS class name for the icon, e.g.
font: {
family: 'typicons',
cssClass: function(iconName) {
return 'typcn typcn-' + iconName;
}
}
You can set an alternative font as the default in the same way you would override other defaults:
$.fn.awesomeCursor.defaults.font = {
family: 'typicons',
cssClass: 'typcn typcn-%s'
};
Hover your cursor over the boxes to see the example cursors
$('#example1')
.awesomeCursor('paper-plane', {
color: '#2cb2da',
hotspot: 'top right'
});
$('#example2')
.awesomeCursor('paint-brush', {
color: '#34db33',
size: 32
});
$('#example3')
.awesomeCursor('ban', {
color: 'red',
size: 24,
hotspot: 'center'
});
$('#example4')
.awesomeCursor('fighter-jet', {
color: 'hotpink',
size: 24,
flip: 'horizontal'
});
$('#example5')
.awesomeCursor('hand-o-up', {
color: 'skyblue',
size: 24,
rotate: 45
});
$('#example6')
.awesomeCursor('pencil', {
color: 'limegreen',
size: 24,
outline: 'brown'
});
Wait for the <iframe>
to load, then set the cursor on the contents:
var $iframe = $('#iframe');
$iframe.ready(function(e) {
$iframe.contents().find('img').awesomeCursor('map-marker', {
color: 'maroon',
size: 32,
outline: '#ccc',
hotspot: 'center bottom'
});
});
Using the Google Maps Javascript API v3.
var map = new google.maps.Map($('#google-maps')[0], {
center: { lat: 37.544494, lng: 138.276398 },
zoom: 8
});
google.maps.event.addListenerOnce(map, 'bounds_changed', function() {
$('#google-maps').find('div').awesomeCursor('location-arrow', {
size: 22,
color: 'orange',
flip: 'horizontal'
});
});