JavaScript Cursor
Posted by Superadmin on May 03 2023 14:26:17

JavaScript Cursor

By Priya PedamkarPriya Pedamkar
  

JavaScript Cursor

Introduction to JavaScript Cursor

JavaScript cursor is a thing used as a mouse cursor whenever it’s going to point on specific elements. There are various types of cursor available those are like wait, help, move, pointer, crosshair, cell, not allowed, zoom-in, zoom-out, etc. This cursor can be changed by assigning value to document.body.style.cursor. JavaScript also helps us to hide the cursor, it should be hide cursor from a webpage, hide cursor for a particular element, and with the help of CSS we are able to set the cursor to none. By using JavaScript along with CSS we can create varieties of a different cursor. We will see all those one by one in detail later.

What is JavaScript Cursor?

JavaScript having different kinds of predefined cursor already available. Some of them going to be used with CSS for proper positioning as well as with better user experience.

Cursor in JavaScript going to be Used with Different Values as follows:

Syntax:

To use JavaScript cursor in our code we are going to use it with the following syntax:

object.style.cursor ="nameOfCursor";

Syntax:

object.style.cursor = value;

Syntax:

<style>
#element_id{
cursor: none;
}
</style>

How Do We Use JavaScript Cursor?

There are multiple ways through which we can use cursor in JavaScript, let’s see all those types one by one in details as follows description:

Examples to Implement JavaScript Cursor

Below are the examples of JavaScript Cursor:

Example #1

This can be one for the entire webpage or on a specific selected element from the page. Here we are hiding cursor from the whole webpage. Code for this is as follows:

Code:

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Cursor</title>
<style>
{
cursor: none;
}
</style>
</head>
<body>
<h4>Hide cursor example</h4>
<div>Hover mouse here to see output:</div>
</body>
</html>

Output:

HTML Code Example 1

Example #2

This is a second example where we are checking cursor for wait. So it will look exactly as shown in the output screen below. Same thing we can use for showing processing cursor.

Code:

<html>
<body>
<p id="cursor">Hover the mouse on the text after clicking button to see changed cursor.</p>
<button type="button" onclick="Change()">Click to change Cursor</button>
<script>
function Change() {
document.getElementById("cursor").style.cursor = "wait";
}
</script>
</body>
</html>

Output:

JavaScript Cursor Example 2

Example #3

This is the example of a combination of multiple cursors going to use in Javascript. Let’s see output for a particular element on which we are going to hover.

Code:

<!DOCTYPE html>
<html>
<head>
<style>
.alias {cursor: alias;
}
.all-scroll {cursor: all-scroll;
}
.cell {cursor: cell;
}
.col-resize {cursor: col-resize;
}
.grab {cursor: -webkit-grab; cursor: grab;
}
.copy {cursor: copy;
}
.crosshair {cursor: crosshair;
}
.e-resize {cursor: e-resize;
}
.help {cursor: help;
}
.ew-resize {cursor: ew-resize;
}
.move {cursor: move;
}
.zoom-in {cursor:zoom-in;
}
.zoom-out {cursor: zoom-out;
}
</style>
</head>
<body>
<h3>The Javascript Cursor with different property Value </h3>
<p>Hover the mouse on particular option to see changeable Cursor</p>
<h4 class="alias">alias</h4>
<h4 class="all-scroll">all-scroll</h4>
<h4 class="cell">cell</h4>
<h4 class="col-resize">col-resize</h4>
<h4 class="grab">grab</h4>
<h4 class="copy">copy</h4>
<h4 class="crosshair">crosshair</h4>
<h4 class="e-resize">e-resize</h4>
<h4 class="help">help</h4>
<h4 class="move">move</h4>
<h4 class="zoom-in">zoom-in</h4>
<h4 class="zoom-out">zoom-out</h4>
</body>
</html>

Output:

Property Value Example 3

Example #4

Code:

<html>
<body>
<h4 id="cell_cur">hover mouse here to see Cell cursor.</h4>
<button type="button" onclick="Change()">Cell Cursor</button>
<script>
function Change() {
document.getElementById("cell_cur").style.cursor = "cell";
}
</script>
</body>
</html>

Output:

JavaScript Cursor Example 4

Conclusion

From all the above information, we have seen the actual use of cursor along with its different types of values. So one can use cursor value as per their appropriate use of the element on the webpage.