Open In App

Pointer Events in Javascript DOM

Last Updated : 26 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Pointer events are a set of DOM (Document Object Model) events that provide a unified way of handling inputs from a variety of devices, such as touchscreens, mouse, and pen/stylus. These events are supported by modern browsers and allow developers to write code that responds to user interactions with the page in a consistent and predictable way, regardless of the type of input device being used.

Some of the most commonly used pointer events include:

  • pointerdown: The event fired when the user presses their pointer on an element
  • pointerup: The event fired when the user releases their pointer from an element
  • pointermove: The event fired when the user moves their pointer over an element
  • pointerover: The event fired when the user moves their pointer over an element
  • pointerout: The event  fired when the user moves their pointer out of an element
  • pointercancel: The event fired when the user's pointer interaction is interrupted (such as by switching to a different application

Syntax:

  • pointerdown property : 
<element pointerdown ="ID">
  • pointerup property :
<element pointerup ="ID">
  • pointermove property :
<element pointermove ="ID">
  • pointerover property :
<element pointerover= "ID">
  • pointerout property :
<element pointerout = "ID">
  • pointercancel property :
<element pointercancel= "ID">

Why pointer events are better than mouse events?

Pointer events and mouse events are both used for handling user interactions with a webpage, such as clicks, hover, and scrolling. However, pointer events are generally considered to be better than mouse events for several reasons:

  • Pointer events provide a more consistent experience across devices: Pointer events are designed to work with a wide range of input devices, including touchscreens, mouse, and stylus. 
  • Pointer events provide more information: Pointer events provide more information about the input device, such as its type (e.g. mouse, touch, pen), pressure, and tilt. 
  • Pointer events are more efficient: Pointer events are designed to be more memory-efficient and have lower overhead than mouse events. 
  • Pointer events are more flexible: Pointer events are more flexible than mouse events in that they can be used to handle both single and multi-touch interactions.

Example 1: Below program illustrate pointermove and pointerout property:

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pointer Events in JavaScript DOM</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #e9ecef;
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            color: #495057;
        }

        .container {
            text-align: center;
            background: #ffffff;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
            max-width: 600px;
            width: 90%;
        }

        h1 {
            color: #007bff;
            margin-bottom: 20px;
        }

        p {
            font-size: 1.1em;
            line-height: 1.6;
            color: #6c757d;
            transition: color 0.3s ease;
        }

        footer {
            margin-top: 20px;
            font-size: 0.9em;
            color: #14ec38;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>Welcome to GeeksforGeeks</h1>
        <p id="myP" onpointermove="pointerMove()" onpointerout="pointerOut()">
            The pointerMove() function works when the pointer 
            moves over this text and changes the text color to 'red'.
            The pointerOut() function works when the pointer 
            leaves this text zone and changes the text color to 'blue'.
        </p>
        <footer>© 2024 GeeksforGeeks. All rights reserved.</footer>
    </div>
    <script>
        function pointerMove() {
            document.getElementById("myP").style.color = "red";
        }
        function pointerOut() {
            document.getElementById("myP").style.color = "blue";
        }
    </script>
</body>

</html>

Output: 

Example 2: Below program illustrate pointerup and pointerdown and pointerover properties:

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pointer Events in JavaScript DOM</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f8f9fa;
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            color: #343a40;
        }

        .container {
            text-align: center;
            background: #ffffff;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
            max-width: 600px;
            width: 90%;
        }

        h1 {
            color: #007bff;
            margin-bottom: 20px;
        }

        p {
            font-size: 1.1em;
            line-height: 1.6;
            color: #495057;
            transition: color 0.3s ease;
        }

        footer {
            margin-top: 20px;
            font-size: 0.9em;
            color: #adb5bd;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>Welcome to GeeksforGeeks</h1>
        <p id="myP" onpointerup="pointerUp()" onpointerdown="pointerDown()" onpointerover="pointerOver()">
            Pointer events are a set of DOM (Document Object Model) 
            events that provide a unified way of handling inputs
            from a variety of devices, such as touchscreens, 
            mouse, and pen/stylus. These events are supported by modern
            browsers and allow developers to write code that 
            responds to user interactions with the page in a consistent
            and predictable way, regardless of the type of input device being used.
        </p>
        <footer>© 2024 GeeksforGeeks. All rights reserved.</footer>
    </div>
    <script>
        function pointerUp() {
            document.getElementById("myP").style.color = "red";
        }
        function pointerDown() {
            document.getElementById("myP").style.color = "blue";
        }
        function pointerOver() {
            document.getElementById("myP").style.color = "green";
        }
    </script>
</body>

</html>

Output: 

  • When the pointer is moved over the text, the pointerOver() function is called. This function changes the color of the text to  "green".
  • Similarly, when the pointer is clicked on the text, the pointerDown() function is called. This function sets the color "blue" to the text.
  • Lastly, when the pointer is released after clicking on the text, the pointerUp() function is called. This function sets the color "red" to the text.
  • So, The overall effect is when the pointer is moved over the text, the text color changes to green, when the pointer is pressed on the text, the text color changes to blue and when the pointer is released on the text, the text color changes to red.

Next Article

Similar Reads