1
1
/**
2
- * Simple element cursor plugin v1.0 .0
2
+ * Simple element cursor plugin v1.1 .0
3
3
*
4
4
* Copyright (c) 2018 Levi Cole <me@thelevicole.com>
5
5
* Licensed under MIT (http://opensource.org/licenses/MIT)
16
16
preserveCursor : false
17
17
} , options ) ;
18
18
19
+ // Set default element styles
19
20
$cursor . css ( {
20
21
'position' : 'fixed' ,
21
22
'pointer-events' : 'none' ,
22
23
'z-index' : 99999
23
24
} ) ;
24
25
25
- if ( ! options . preserve_cursor ) {
26
+ // Hide default browser cursor
27
+ if ( ! options . preserveCursor ) {
26
28
$ ( 'head' ) . append ( $ ( '<style>' , {
27
29
text : '*, *:before, *:after { cursor: none; }'
28
30
} ) ) ;
29
31
}
30
32
33
+ // Define global variables
31
34
let mouse_x = 0 ,
32
35
mouse_y = 0 ;
33
36
37
40
let id = '' ,
38
41
node = '' ;
39
42
40
- const move = ( ) => {
41
- // Convert cursor position into percentage
42
- const x_percent = ( mouse_x + options . offsetX ) / window_x * 100 ;
43
- const y_percent = ( mouse_y + options . offsetY ) / window_y * 100 ;
44
-
45
- $cursor . show ( ) . css ( {
46
- top : y_percent + '%' ,
47
- left : x_percent + '%'
48
- } ) ;
49
-
50
- $cursor . attr ( 'data-id' , id ) ;
51
- $cursor . attr ( 'data-el' , node ) ;
52
- } ;
53
-
54
- $ ( window ) . on ( 'resize' , function ( event ) {
55
- window_x = $ ( window ) . width ( ) ;
56
- window_y = $ ( window ) . height ( ) ;
57
-
58
- move ( ) ;
59
- } ) ;
60
-
43
+ // Update global variables on document events
61
44
$ ( document ) . on ( 'ready mousemove resize' , function ( event ) {
62
45
const $target = $ ( event . target ) ;
63
46
64
- mouse_x = event . clientX ;
65
- mouse_y = event . clientY ;
47
+ window_x = $ ( window ) . width ( ) ;
48
+ window_y = $ ( window ) . height ( ) ;
49
+
50
+ mouse_x = event . clientX ;
51
+ mouse_y = event . clientY ;
66
52
67
- id = $target . attr ( 'id' ) || $target . closest ( '[id]' ) . attr ( 'id' ) || '' ;
68
- node = $target . prop ( 'nodeName' ) . toLowerCase ( ) ;
69
-
70
- move ( ) ;
53
+ id = $target . attr ( 'id' ) || $target . closest ( '[id]' ) . attr ( 'id' ) || '' ;
54
+ node = $target . prop ( 'nodeName' ) . toLowerCase ( ) ;
71
55
} ) ;
72
56
57
+ // Hide element if cursor leaves the document
73
58
$ ( document ) . on ( 'mouseleave' , function ( ) {
74
59
$cursor . hide ( ) ;
75
60
} ) ;
76
61
62
+ // Hide element if cursor enters an iframe
77
63
$ ( 'iframe' ) . on ( 'mouseenter' , function ( ) {
78
64
$cursor . hide ( ) ;
79
65
} ) ;
80
66
67
+ // Add click event to element
81
68
$ ( document ) . on ( 'mousedown mouseup' , function ( event ) {
82
69
let event_name = '' ;
83
70
87
74
88
75
$cursor . attr ( 'data-event' , event_name ) ;
89
76
} ) ;
77
+
78
+ /**
79
+ * Tell the browser that we wish to perform an animation
80
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame
81
+ */
82
+ window . requestAnimationFrame ( function animation ( ) {
83
+
84
+ // Convert cursor position into percentage
85
+ const x_percent = ( mouse_x + options . offsetX ) / window_x * 100 ;
86
+ const y_percent = ( mouse_y + options . offsetY ) / window_y * 100 ;
87
+
88
+ $cursor . show ( ) . css ( {
89
+ top : y_percent + '%' ,
90
+ left : x_percent + '%'
91
+ } ) ;
92
+
93
+ $cursor . attr ( 'data-id' , id ) ;
94
+ $cursor . attr ( 'data-el' , node ) ;
95
+
96
+ window . requestAnimationFrame ( animation ) ;
97
+
98
+ } ) ;
90
99
} ;
91
100
92
101
} ) ( jQuery || window . jQuery ) ;
0 commit comments