Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery event.pageY Property

Posted in jQuery Tutorial
Updated on Nov 21, 2024
By Mari Selvan
πŸ‘οΈ 37 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
jQuery event.pageY

Photo Credit to CodeToFun

πŸ™‹ Introduction

jQuery offers a wide array of properties and methods to enhance web development, particularly for handling events. One such property is event.pageY, which provides the vertical coordinate of an event relative to the whole document. This property is particularly useful for tracking mouse movements and interactions on a webpage.

In this guide, we'll explore the event.pageY property with clear examples to help you leverage its potential in your projects.

🧠 Understanding event.pageY Property

The event.pageY property returns the vertical position of the mouse pointer when an event is triggered, measured in pixels from the top edge of the document. This property is part of the event object in jQuery, which contains information about the event that was triggered.

πŸ’‘ Syntax

The syntax for the event.pageY property is straightforward:

syntax.js
Copied
Copy To Clipboard
event.pageY

πŸ“ Example

  1. Getting the Vertical Position of a Click:

    Suppose you want to display the vertical position where a user clicks on the document. You can use the event.pageY property to achieve this:

    index.html
    Copied
    Copy To Clipboard
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>event.pageY Example</title>
      <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    </head>
    <body>
      <h1>Click anywhere on the document</h1>
      <script>
        $(document).click(function(event) {
          alert("Vertical position: " + event.pageY + "px");
        });
      </script>
    </body>
    </html>

    This script will display an alert showing the vertical position in pixels whenever the user clicks anywhere on the document.

  2. Tracking Mouse Movement:

    You can use the event.pageY property to track the vertical position of the mouse as it moves across the document. Here’s an example:

    index.html
    Copied
    Copy To Clipboard
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Track Mouse Movement</title>
      <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    </head>
    <body>
      <h1>Move your mouse around the document</h1>
      <p id="position"></p>
      <script>
        $(document).mousemove(function(event) {
          $("#position").text("Vertical position: " + event.pageY + "px");
        });
      </script>
    </body>
    </html>

    This code will continuously update a paragraph element with the vertical position of the mouse as it moves.

  3. Using event.pageY in a Drag and Drop Interface:

    When creating a drag-and-drop interface, you might want to know the vertical position of the mouse to position elements correctly. Here’s a basic example:

    index.html
    Copied
    Copy To Clipboard
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Drag and Drop Example</title>
      <style>
        #draggable {
          width: 100px;
          height: 100px;
          background-color: lightblue;
          position: absolute;
        }
      </style>
      <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    </head>
    <body>
      <div id="draggable">Drag me</div>
      <script>
        $("#draggable").on("mousedown", function(event) {
          $(document).on("mousemove.dragging", function(event) {
            $("#draggable").css({
                top: event.pageY - 50 + "px" // Adjusting to center the element on the cursor
            });
          });
        });
    
        $(document).on("mouseup", function() {
          $(document).off("mousemove.dragging");
        });
      </script>
    </body>
    </html>

    This script allows a user to drag a div vertically, updating its position based on the event.pageY property.

  4. Combining event.pageY with Other Event Properties:

    You can combine event.pageY with other properties like event.pageX (for horizontal position) to get comprehensive control over event handling. For example, you can determine the exact position of the mouse pointer in both vertical and horizontal axes.

πŸŽ‰ Conclusion

The jQuery event.pageY property is a valuable tool for obtaining the vertical position of mouse events, which can be utilized in various interactive web applications. Whether you need to display click positions, track mouse movements, or create dynamic drag-and-drop interfaces, event.pageY provides a straightforward solution.

By mastering this property, you can enhance the interactivity and responsiveness of your web pages.

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy